I understand that math operations require more resources/take longer than others when being computed. (e.g. square roots take longer than divisions which takes longer than multiplications which takes longer than additions)

Are there known times for how long specific operations take or methods with which to measure said times? (I'm guessing no for the former based on the wide variety of hardware that exists. I'm technically looking for measurements related to Game Maker: Studio 2 if it matters)

(P.S. Sorry if the tags for this question are wrong.)

  • \$\begingroup\$ In what kind of context do you need such data? Have you encountered issues in what you're doing? Have you tried to measure it yourself? \$\endgroup\$ – Vaillancourt Feb 21 '19 at 2:09
  • \$\begingroup\$ I haven't run into any issues with anything I'm doing. It's mostly just curiosity. I know of "John Carmack's Inverse Square Root" from the Quake 3 engine that came about because more efficient use of resources was needed for 3d games at the time. I think the knowing math tricks like this would be a good thing to know. \$\endgroup\$ – Dr Negative Feb 21 '19 at 2:23
  • \$\begingroup\$ 12 parsecs. Or more seriously - modern FPUs are incredibly complex, so you really need to profile your own use cases. The Quake3 inverse square root is a notorious example because it's actually likely to be faster to just do 1/sqrt on modern hardware. \$\endgroup\$ – Maximus Minimus Feb 21 '19 at 9:59

There aren't known times for mathematics operations that are consistent across all devices, since speeds on each machine will vary, but it is known that certain mathematical operations will always take longer than others. Here is a list of mathematical operations in order from Fastest to Slowest:

  • Add / Subtract / Multiply by power of 2
  • Divide by power of 2
  • Modulo by power of 2
  • Multiply
  • Divide
  • Modulo
  • Square root
  • System Functions (sin, cos, tan & other library functions like floor & ceil)
  • User Functions

This list is derived from how computers perform mathematical operations on a bit by bit level.

Furthermore, certain types of loops are more efficient than others. Here they are listed Fastest to Slowest:

  • While
  • If
  • Fixed / Static function calls
  • Switch
  • Indirect function calls (virtual or dynamic binding)

If you'd like to test the speed of a particular section of code, put the code in a loop and implement a timer to start before the loop, stop after the loop, and display the total elapsed time. Since computers can do most math so quickly a timer has trouble measuring it, we increase the loop to run many times; hundreds or even thousands of times. This way we can measure the elapsed time in seconds and milliseconds rather than nanoseconds. An operation that is only nanoseconds more efficient by comparison if ran once will be milliseconds or full seconds faster by comparison when ran thousands of times.

For extra info, check out "Big O Notation" and here's a cool sorting visualization.

I am compelled to add two things: 1) get everything working before you try to optimize and 2) use the timer and loop technique (aka build a profiler) or use the game engine profiling tools to identify which areas are slow, rather than guessing. Changing how you're doing mathematical operations will save you very little when it comes to performance. Much larger gains can be achieved by fixing logic, adding LODs to models, less raycasting etc.

| improve this answer | |

You don't measure operations in time, because it only takes a couple of nanoseconds and it changes based on the CPUs clock speed. You should use cycles instead, these generally tell you how many steps it takes for a CPU to do an operation (this is a simplified explanation, but it should get the point across).

Addition and subtraction only generally take 1 or 2 cycles, multiplication takes 2-3, division is 10-20, square root is a ~100, trig functions are a couple hundred (if you want more detail, look into the intel optimization manual). Although these might seem like a lot, given how CPUs run at 4GHz (4 million cycles per second) these days, it's really not a lot.

However, in the case of game maker it's much more difficult to guess. It's not clear whether Game Maker uses an interpreter or compiler nowadays (it has used interpreter in older versions and we don't know whether they changed it since), so let's go with the worst case scenario and say it's an interpreter.

Interpreters (instead of taking your code and converting it to machine code) take your code, divide it into tokens and go through them one by one, executing the necessary operations. These require a large amount of branching and equality checking, so they can't match the speed of compiled programs. It's next to impossible to know how much time they need per operation.

However, since we're still working with modern, fast CPUs, stronger than the combined computing power of the Apollo program and using GPUs to make graphics even faster, if a 2d game maker game starts lagging, you most likely did something wrong. Make sure to not have 2 million particles in each scene.

| improve this answer | |
  • \$\begingroup\$ AFAIK both GMS and GMS2 use both a VM to execute a game and a compiler (the YoYo Compiler) to generate a target-specific executable; the choice is left to the user. \$\endgroup\$ – liggiorgio Feb 25 '19 at 13:52

Generally speaking (i.e. not just GameMaker), there are few ops / scenarios you need to watch out for, in rough order of precedence:

  • Cache misses (hard for you tell when this is happening in GM, so don't worry much about this)
  • Conditionals, especially deeply nested if / for / while / ternary operator
  • Trig functions sin, cos etc.
  • sqrt
  • Division / Modulus

Multiplication is really not a concern, same for addition and subtraction. Bitwise / logical operators are even cheaper to process, generally.

| improve this answer | |

It boils down to if the algorithm to compute the operator is parallelizable or not, as multiplication is "glorified" addition there is no problem in computing it on different instructions, if you are doing division you need the last operation to go on to the next one so instruction-level parallelism is not so straight forward. Search on SO why is division slower than multiplication.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.