• Advertisement
Sign in to follow this  

New is SLOW!

This topic is 1875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


Also, I have never heard the word "noddy."


:)

Not sure if it's just British English, but it's slang for "simplistic".

Share this post


Link to post
Share on other sites
Advertisement
The best way to look at the allocation speed issue, in my opinion, is this: Your computer (or your customer's computer) does so and so many things in so and so much time. Sometimes that matters, and sometimes you just couldn't care less.

My system does 100 million new/delete pairs in 12.3 seconds. Which means no more and no less than if I do 8130 allocations per second (at 60 FPS, that's 130 allocations per frame), this will eat 1 millisecond. If I do 813, it will only eat 0.1 milliseconds. If 0.1 milliseconds are OK, then why bother? I don't need to care. When your program runs fast enough so it's capped by vertical sync, there's no gain if it starts blocking half a millisecond earlier.

On the other hand, if application performance tells me (and if profiling shows that it's a significant factor at all!) that I can't afford those 0.1 milliseconds, I have to make a plan to do fewer allocations. Edited by samoth

Share this post


Link to post
Share on other sites
Hello all.

I just ran the following two functions 100 times each ...


When using MSVC your code is translated like this:
[source lang="plain"]?passing@@YAXH@Z (void __cdecl passing(int)):
00000000: C3 ret

?referrencing@@YAXPAH@Z (void __cdecl referrencing(int *)):
00000010: 56 push esi
00000011: BE 40 42 0F 00 mov esi,0F4240h
00000016: 6A 04 push 4
00000018: E8 00 00 00 00 call ??2@YAPAXI@Z
0000001D: 50 push eax
0000001E: E8 00 00 00 00 call ??3@YAXPAX@Z
00000023: 83 C4 08 add esp,8
00000026: 4E dec esi
00000027: 75 ED jne 00000016
00000029: 5E pop esi
0000002A: C3 ret[/source]
As you can see, the first function, [font=courier new,courier,monospace]passing[/font], is reduced to a single [font=courier new,courier,monospace]ret[/font] instruction making it equivalent to this one:
[source lang="cpp"]void passing(int j )
{
}[/source]
In other words, through optimization algorithms the comiler "concludes" (notice the qutation marks because we a talking about a piece of software) that the loop is redundant and removes it entirely from the function body. However, in [font=courier new,courier,monospace]referrencing[/font] the loop is translated because two external functions (the [font=courier new,courier,monospace]int[/font] constructor and -destructor) are called in each iteration. And even though it may seem like this could be optimized out of the loop as well, as soon as you invoke an external function things change quite significantly from a complier's "point of view" because the function's implementation isn't part of your code and is stored in binary form in some library (or object) file on your hard-drive.

I hope this answers your question.

Share this post


Link to post
Share on other sites

However, in referrencing the loop is translated because two external functions (the int constructor and -destructor) are called in each iteration.

The int constructor and destructor are trivial and not present in the compiler's output. The two function calls you see are the memory allocation and deallocation.

Share this post


Link to post
Share on other sites

[quote name='Dragonion' timestamp='1354644568' post='5007148']
However, in referrencing the loop is translated because two external functions (the int constructor and -destructor) are called in each iteration.

The int constructor and destructor are trivial and not present in the compiler's output. The two function calls you see are the memory allocation and deallocation.
[/quote]

True. But for the sake of simplicity I took the liberty of using the words "constructor" and "destructor" as aliases for the entire memory allocation/de-allocation processes they initiate when you use them. Edited by Dragonion

Share this post


Link to post
Share on other sites
As the assembly Dragonion posted shows:

It just goes to show that nothing is faster than something, and something is slower than nothing.blink.png Edited by iMalc

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement