Archived

This topic is now archived and is closed to further replies.

Thrump

int's, int's, everywhere!

Recommended Posts

I started a project in C a while ago, and any time I need an integer, I used int, regardless of the size needed. Now looking back, I want to start changing them to a more logical size. My question is, does the size of arguments affect speed of a function call? I know generally yes, but is void Function(Int32); slower than void Function(Int8); How does this depend on the processor''s word size?

Share this post


Link to post
Share on other sites
most likely the compiler will pass "int8" as a 32-bit integer (assuming Intel x86 architecture), it will just sign/zero extend it.

this all really depends on a CPU''s ability to manipulate data not equal to the processor word size.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
No, I think it wouldn''t.
But I can''t really know that because I don''t know your target hardware.

If you''re using an 8-bit integer, it wouldn''t be faster but your program would be smaller.

Of course there is some dependence of size and speed and if programs are smaller they sometimes execute faster ( if you''re using more memory ( RAM ) then available and others ... ).
But usually the first choice ( 32-bit ) would be faster.

One thing you have to know is your goal and that there''s nearly always a possibility to trade size for speed.

And if you really want to know why this is like it is,
you''ll have to study your target processor ( to look up the size of the data bus, the execution unit and stuff like that ) and your algo
( for checking if it wouldn''t be faster when you use precalculated / preprocessed data ).

Share this post


Link to post
Share on other sites
Askadar: "Mordern computers are optimized for 32bit dwords."

are you sure?

i would remove the word "computers" and replace it with "Windows based PC''s".

because the Itanium, various Alpha chips, various MIPS RISC chips (including the PS2), etc. are NOT 32-bit.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
AP: actually what VC++ 6.0 (enterprise) does is if the value is a constant (i.e. 10), it uses "push 0ah". if the value is a variable (i.e. __int8 j) it uses "mov al, [j]" & "push eax".

also if i define a function "__cdecl void func(__int8 i)".
when the compiler cleans up the stack is uses "add esp, 4".

this is because Windows wants to keep the stack 32-bit aligned.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
yeah, as well as MIPS, PowerPC, Motorola 68xxx, etc.

my point is Visual Studio optimizes for 32-bit because majority of Windows users are using Intel Pentium base processors.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites