Dumb question

D619d95cddb1edb227f51ef539d15cdc
0
Nautilus 103 Mar 03, 2005 at 14:10

I’m writing a function which has to determine and return the visibility status for (3D) objects.

Now, you can picture how many times per second this function is being executed.
Speed is critical.

A lot of optimizations went into it and I ended up using 16 flags (besides the other variables) which get declared, used and destroyed every time the function code is being called, executed and terminated.
These flags simply need to hold a 2-state value, wether it is true/false or 0/1 makes no difference.

I can’t make up my mind about the data type of these flags.

I made some simple benchmark test using both bool and dword for the flags.
Surprisingly the benchmarks showed pretty much the same results (100 millions operations in roughly 6.2 seconds -on a Intel PentiumIII 1 GHz-).

At first I ran the bool bench, and then the dword bench.
They outputted a 6.2 secs for bool and over 7.4 for dword.

But then I ran dword bench before bool bench and now the output was 6.2 for dword and, again, over 7.4 for bool :wacko:

So it seems it’s a sort of first come first serve: whoever runs first gets the best result (might be due to CPU heat raising during the 1st test?)
Well, in the end both data types give the same results.

So I can’t decide, what is better: dword or bool?

Thanks for reading :)
Regards.

8 Replies

Please log in or register to post a reply.

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Mar 03, 2005 at 14:48

most likely you are doing something wrong in your testing setup… are you using printf inside your profiling ? in general a dword is one memory word for your computer so there will be no gain in speed if you use a one byte variable rather than a 4byte one.

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Mar 03, 2005 at 14:49

you might want to pack more than one flag into your dowrd though and use bit operations on them. that will most likely bring you some speed gain because you have less memory access

D619d95cddb1edb227f51ef539d15cdc
0
Nautilus 103 Mar 03, 2005 at 15:55

Yes, I could pack all flags in a single dword and save memory.
Frankly, I could just discard the whole flag thing and keep working on bitmasks (as the rest of the function does).

The why I came up with the flags thing is a complicated one (a strange idea born from strange reasoning), not worth explaining it.

I’ll have discarded it anyway, but I happened to wonder which was faster between bool and dword data types.

Speaking of variables, perhaps you can answer another question o’mine.
You know, I never really got how variables are held and treated in memory after .exe compilation.

Suppose you have a function. Inside the function are declarated a number of variables (just plain variables, no arrays). Few or many doesn’t matter.
Once you compile the program, how are these variables handled?

Every time you call this function:
A) the program need to allocate memory for the variables -on the fly- (by searching for an empty place in memory big enough to contain all variables)?

-or-

B) the program already has all the necessary memory ready and just access it as needed?

I hope you understand what I mean ‘cause I’m not too good with english…
Shame on me for asking stupid things :blush:

Regards.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Mar 03, 2005 at 18:43

Variables that are declared local in your function will be either be kept in registers and never see memory, or they will be allocated on the stack.

I don’t know how much you know about the internal workings of function calls, but the stack is an area of memory that grows when you make a function call and shrinks when the function returns; it stores the return address to jump to, as well as parameters to the function whatever local variables the function has, and sometimes the function’s return value.

Local variables, the size of which are known at compile time, can be allocated on the stack. But when you use new and delete to dynamically allocate memory (size not known until runtime) it comes from a different area of memory called the heap.

D619d95cddb1edb227f51ef539d15cdc
0
Nautilus 103 Mar 04, 2005 at 14:00

Touchè! :tongue:
I don’t know much about function call workings :happy:
Just heard of heap and stack but never read good explanations on ‘em.

Correct me if I’m wrong, please.
If my function is not recursive, doesn’t use arrays (not static, nor dynamic) and all variables declared in it are local ones, then every variable is kept in the stack. Right?

And should I use dynamic arrays, these arrays would be kept in the heap.
I know that dynamic arrays are slower than static ones. Is it because they are located in the heap? Or am I all wrong?

Thank you for explanaining :)

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Mar 04, 2005 at 15:00

If my function is not recursive, doesn’t use arrays

recursion happens on the stack, too (actually that’s one of the benefits of having the stack). arrays are allocated on the stack if they are not dynamic as well.

I know that dynamic arrays are slower than static ones

an array is just a pointer to a memory location. there is no difference in speed between using dynamic and static arrays. what you might have heard is that dynmic arrays take longer to allocate. this is because on the stack you can just push say 10 bytes on the stack and use that space as an array. later you just pop that memory from the stack again. with dynamic memory allocations things are way more complicated because allocation and deallocation might happen at any time in the program so more complex algorithms and data structures are required to keep track of the memory in use.

example :

<dynamic space>
[10 bytes of dynamically allocated memory]
[20 bytes of …]
[5 bytes of …]
[1mb of free memory]
</dynamic space>

now you deallocate the 20 bytes again….

<dynamic space>
[10 bytes of dynamically allocated memory]
[20 bytes of free memory]
[5 bytes of …]
[1mb of free memory]
</dynamic space>

if you now want to allocate another 15 bytes you could just take memory from the 1mb of free space at the end. this would of course rip a whole in your memory that doesn’t get filled up anymore and thus is wasted. you can imagine that that would be a total waste of memory over a longer period of time. several algorithms exist to reduce this effect (which is called fragmentation). because of the increased book keeping that is needed to reduce the fragmentation however each memory allocation takes a fair amount of time to return the allocated memory to you.

D619d95cddb1edb227f51ef539d15cdc
0
Nautilus 103 Mar 04, 2005 at 15:35

I see.
So in general, one should keep dynamic arrays allocations at the strict necessary.
Therefore, where possible, it is preferrable to ‘waste’ some memory by using a more suitable (bigger) static array, rather than using many smaller dynamic ones.

But once a dynamic array is allocated there’s no difference in performance (compared to a static one) unless a continuos cycle of release and reallocation happens.
Correct?

Thank you Anubis :)

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Mar 04, 2005 at 16:05

right… !