Which Is Faster?


zacaj

void main()
Joined
Apr 3, 2007
Messages
362
Age
30
Location
NY
Website
zacaj.com
What would be faster?
What would be better(less resource use, etc)?

A switch statement that chooses what function to run based on a variable, or an array or pointers to the functions?

Dividing, or making a 2D array of answers to division?

An if with multiple questions ( if(g>i&&j<h) ) or two if's ( if(g>i) if(j<h) )?

Drawing a grid, or drawing a picture of one?
 
as far I know fastest way is to try to keep tiles/data n^2 size, it makes faster to calculate that stuff with >> and << rather than with * and / %-s

I believe calling function through pointer is 1.125 times slower

mush stuff also depends on compiler

OOP is heavy resource eater too

I hope that was any of help
 
zacaj said:
What would be faster?
What would be better(less resource use, etc)?
Depends a lot on the compiler as well as other stuff in your code. Best to profile everything always.

zacaj said:
A switch statement that chooses what function to run based on a variable, or an array or pointers to the functions?
Based on my testing with my 8051 emulator, which basically just takes an opcode and then figures out what to do with it, there's little difference. The switch basically compiles into a jump table anyway in most cases, and whichever you choose, you get the worst case scenario for branch prediction. Switch structure may be more "safe" in the way that there's less things that can go wrong, but your mileage may vary.

zacaj said:
Dividing, or making a 2D array of answers to division?
This depends a lot on the compiler. The table is probably the worst choice unless it's very small and used very often so that it fits into the cache.
Instead, if this is integer division, consider doing it with shifts (any integer division can be split into shifts, additions and substractions), and if non-integers, multiply by the reciprocal of the divisor (i.e. instead of v = x / y, do z = 1/y, v = x * z). There are plenty of math tricks around - search the web.

zacaj said:
An if with multiple questions ( if(g>i&&j<h) ) or two if's ( if(g>i) if(j<h) )?
The C compiler has an "early out" mechanism, so if you have, for instance if (foo() && bar()) and foo returns false, bar() is never called. But to answer the question, both are probably just as fast. Modern compilers should turn those into an identical data-flow graph, which is then used to actually generate code.

zacaj said:
Drawing a grid, or drawing a picture of one?
Depends a lot on what this grid is =). If the algorithm to figure out the grid is simple enough, it'll stay inside the CPU cache - reading from the picture will probably trash the data cache.
 
Last edited by a moderator:
zacaj said:
Dividing, or making a 2D array of answers to division?
I believe an array would be quite faster than division in the ARM. But there are other ways to avoid the division as the method mention already, to precalculate in fixed point the 1/Z (if you can precalculate it only once or very few times outside the main loop), and then multiply this value to do the division (because integer multiplications on the ARM takes few cycles, the rest (divisions, floating points) are simulated). Though in some of my code the value by which I divided changed at each loop in my main loop so I made some not so big array of 65536 * (1/Z) integer fixed point values for Z=0 to 4096 for example, multiplied by the array value and at the end >> 16 to go back from fixed point to the true value at the end of the calculation. It works preety well and fast enough.

zacaj said:
Drawing a grid, or drawing a picture of one?
If the grid has only horizontal and vertical lines it will be most probably faster. I would go for drawing the grid in this case.
 
Last edited by a moderator:
Back
Top