Data Types/Data Type Efficiency
When I return values, usually to test to see if a function worked or not i.e.: if (!function)
OutputText("This function failed");
I always make the return of type int. Is this the most efficient method. I believe that bool, char, and byte would work just as well (after all, there are only 2 possible answers, 0, or >0), but which one would be fastest and (more importantly for data types) use the least amount of memory/space?
Thank ahead of time!
Aurelio Reis
"Artificial Intelligence: the art of making computers that behave like the ones in movies."www.CodeFortress.com
I''m not absolutely sure if this is true but I''ve read that Pentiums are optimized to handle DWORD size variables. I could be wrong though.
Justin Meyer
codeheadforever is right. Current processors are 32 bits and therefor handle 32 bits values the best. Even more, if you optimize for speed (and not for size) your boolean will probably end up to be an 32 bit value anyway.
Besides, the return value of a function is put in the register EAX which is a 32 bits register, therefore 32 bits are the fastest. 16 bit and 8 bit values, take more time.
Well, at least that's what I thought . I don't however think that considering the return values type is going to give you any speedups. Look for other optimizations, and if the function is called lots and lots of time then make it inline and small.
For error indicators I use 32 bit values where each value is a different error for ex:
const DWORD ERROR_FATAL = -1;
const DWORD ERROR_OUT_OF_MEMORY = -2;
const DWORD ERROR_FILE_NOT_FOUND = -3;
const DWORD ERROR_NOT_IMPLEMENTED_YET = -4;
const DWORD ERROR_NOT_SUPPORTED_ON_THIS_HARDWARE = -5;
const DWORD ERROR_FIND_YOURSELF_A_BETTER_COMPUTER = -6;
const DWORD ERROR_UNEXPECTED = -7;
const DWORD SUCCES_FINISHED = 1;
etc.
Succes values are positive (you won't need many of those)
and error values are negative. Then you define a macro like this
#define ERROR( e ) e < 0
and you can call your functions like this:
if ( ERROR( function ) )
{
// Handle error;
}
Most of the time you won't need to know what error it was but sometimes it is handy and then you can just look up the exact value.
Anyway, I started rambling too much I noticed, later and goodluck
Jaap Suter
Edited by - s9801758 on August 10, 2000 3:56:24 PM
Besides, the return value of a function is put in the register EAX which is a 32 bits register, therefore 32 bits are the fastest. 16 bit and 8 bit values, take more time.
Well, at least that's what I thought . I don't however think that considering the return values type is going to give you any speedups. Look for other optimizations, and if the function is called lots and lots of time then make it inline and small.
For error indicators I use 32 bit values where each value is a different error for ex:
const DWORD ERROR_FATAL = -1;
const DWORD ERROR_OUT_OF_MEMORY = -2;
const DWORD ERROR_FILE_NOT_FOUND = -3;
const DWORD ERROR_NOT_IMPLEMENTED_YET = -4;
const DWORD ERROR_NOT_SUPPORTED_ON_THIS_HARDWARE = -5;
const DWORD ERROR_FIND_YOURSELF_A_BETTER_COMPUTER = -6;
const DWORD ERROR_UNEXPECTED = -7;
const DWORD SUCCES_FINISHED = 1;
etc.
Succes values are positive (you won't need many of those)
and error values are negative. Then you define a macro like this
#define ERROR( e ) e < 0
and you can call your functions like this:
if ( ERROR( function ) )
{
// Handle error;
}
Most of the time you won't need to know what error it was but sometimes it is handy and then you can just look up the exact value.
Anyway, I started rambling too much I noticed, later and goodluck
Jaap Suter
Edited by - s9801758 on August 10, 2000 3:56:24 PM
____________________________Mmmm, I''ll have to think of one.
Actually, you should use bools whenever they fit appropriately because it makes your code easier to understand. Worried about the performance? Assuming you''re using a recent compiler, don''t be. In ANSI/ISO C++ booleans are automatically promoted to int data type during compilation so you''ll still be getting the best possible performance.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement