Optimization

Started by
4 comments, last by FantasyGuy 22 years, 3 months ago
I have a function that is to be called so frequently and this function receives 3 arguments. Is it faster to pass the arguments as three seperate arguments to that function or is it faster to put the three arguments in one structure and pass a pointer to that structure. The structure is to be filled only once during my program so I don''t need to worry about the time spent filling the structure. Of course, passing one argument is faster than passing three arguments, but accessing the arguments directly is faster than an indirect access (via a pointer) to the three variables. Any comments? Thanks.
Advertisement
It''s a worthless optimzation. Look for real bottlenecks.

However, if you''re insistent: not only will you have to dereference the pointer to the structure, but also dereference the structure members. So 3 separate parameters would, to my mind, be faster.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Agreed,

finish writing the program first using the best *algorithm* for each case, then *PROFILE* to find the bottlenecks and optimise whatever takes the most time.

Otherwise you''ll waste a lot of time going on wild goose chases like this for things you *think* might be slow. Profiling means you can be 100% certain of what to optimise.




--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Oh, I see. Thanks guys.
Passing the three values into a structure and passing the structure''s address is slower than just passing the three values.


However, if you already have the three values in the structure or array, then it will be faster to pass the array/structure pointer UNLESS the values are all characters, or two are characters and the other an integer.


Here is how it goes.

Passing three characters.
1 byte - 1 byte - 1 byte
Passing three ints
2 bytes - 2 bytes - 2 bytes
Passing three longs
4 bytes - 4 bytes - 4 bytes

Meanwhile passing the address of an array of three anythings
4 bytes

That is a simple way to understand that it IS faster to pass the address of an array instead of a bunch of different parameters.


On the other hand as stated above, it normally isn''t a bottle neck. Then again, if you are passing millions of these calls per frame at 70 frames per second, it does make a difference.

Personally I use pointers to the structures as it is faster when using them lots (mainly for maths routines and the gl...fv functions) and it also helps keep it all nice and neat if you use the same variables over and over in different functions.

That or I declare them globally.


Beer - the love catalyst
good ol'' homepage
Beer - the love catalystgood ol' homepage
Oh yeah, passing three values means that the function has to allocate space to hold the variables. Using only pointers doesn''t (you can get around this by using statics as they stay there afterwards). Good for frequently called functions.


Beer - the love catalyst
good ol'' homepage
Beer - the love catalystgood ol' homepage

This topic is closed to new replies.

Advertisement