So I wrote a c program that runs two for loops, recording and printing the time before and after each for loop.
In the first for loop, i just do output = 5/2,
and in the second for loop, i do output = division_method(5, 2)
division_method just looks like this:
division_method(num1, num2)
{
return (num1 / num2)
}
I increase the loop cycles until there is a visible comparison in seconds between the two loops, and obviously the division_method() loop is slower. Normal division takes 65.39 seconds, while the function call takes 91.78 seconds.
I decide to replace the division sign with a multiplication sign just for funsies, so now both loops are doing multiplication.
Normal multiplication - 71.55 seconds (Slower than normal division, faster than function division)
Multiplication returned by a function call - 64.42 seconds. (Fastest overall)
This has me confused for a few reasons. For one, I thought that division was supposed to be a lot slower than multiplication (although I'm very not well researched and have no solid ground for that claim except the Quake evil pointer hack video). Two, why is multiplication wrapped in a function faster?
At first I thought it had to do with variables being close together on the stack, but I tried messing around with variable ordering and I don't know what it is.
Specifically the operation I'm doing is either:
5 / 2,
division_method(5, 2){ return num1 / num2; }, <- i know this isn't correct syntax but just for simplicity sake ima write it like that here
5 * 2,
or division_method(5, 2){ return num1 * num2; } <- im lazy so i didnt change the function name