Retrieve the length of an int
-
You can laugh but I bet that the reason that most Microsoft programs are slow and clunky is because the programmers who write them do bad programming techniques such as that.. and it doesn't matter for that particular example, as that only wastes 19 bytes at the maximum.. but if you apply that technique for other data allocation, then you could waste a lot more which makes your application unreliable and not perform so well.. --PerspX
"Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates
Perspx wrote:
I bet that the reason that most Microsoft programs are slow and clunky...
I highly doubt that. Reserving 1 byte or 20 bytes from the stack will make no noticeable difference in your code's execution time, but using the
log10()
function and asking the memory manager for countless small amounts of memory will. Consider the following:for (int x = 0; x < nFileCount; x++)
{
int nLength = (int) (log10(dwFileSize) + 1.0);
char *s = new char[18 + nLength];
sprintf(s, "File Size: %lu bytes", dwFileSize);
delete [] s;
}compared to:
char s[28];
for (int x = 0; x < nFileCount; x++)
{
sprintf(s, "File Size: %lu bytes", dwFileSize);
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Perspx wrote:
...don't manage resources well
Don't forget, memory is not the only resource a computer has. Sounds to me like you're wasting the CPU time (another resource) in order to save a few bytes of memory. It's hard to imagine a case where those few bytes would ever matter. There's also the factor of code complexity -- sounds like you're making your code more complex than it needs to be. Since you're calculating size, I'd guess you're going to be allocating memory dynamically through malloc or new[] etc. Those schemes will add their own overhead which will swamp the few bytes you're aiming to save.
Perspx wrote:
Don't you agree?
In principal, yes, don't waste resources. However, I think you're going overboard in this case (the other posts seem to agree).
Ok yes I see what you are all saying.. Thanks everyone --PerspX
"Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates