Jump to content

  • Log In with Google      Sign In   
  • Create Account

New is SLOW!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
30 replies to this topic

#21 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 03 December 2012 - 03:02 PM


Thank you very much edd, that is very instructive. I never considered the template version. Can you tell me how I can quote others and post clean and colored code like you did?

Under each post, there's a button that says "Quote". Click it to quote someone. Alternatively, add [ quote ][ /quote ] (without spaces) tags in your post to create your own quote (but it won't display their name or post or timestamp if you don't click "Quote"). To get formatted code, use [ code ][ /code ] tags (again, without spaces)


Thank you very much!

I wonder as I wander...

http://www.davesgameoflife.com


Sponsor:

#22 alnite   Crossbones+   -  Reputation: 2132

Like
0Likes
Like

Posted 03 December 2012 - 03:06 PM

I didn't say it does not come with a price at a different place. If Java-new were just completely better without any negative side effect at another point everyone would be stopping to use C++ and start using Java (well, not really). The important point up there was that new being expensive is something related to C++ only. It comes with prices and benefits all over the place. Other languages, even if they have the exactly same looking new-keyword, do that differently and things come with different prices and benefits.


No. Allocating memory is always expensive regardless of what language you are speaking in, even in Java. I find this a common error among Java programmers. They take GC for granted and they forgot their code is wasting cycles. Allocating new inside a loop is very much a bad habit.

some loop {
    String someStuff = "I'm" + " concatenating " + " like" + "crazy " + "here.";
    MyClass temp = new MyClass();
    // using temp and someStuff
}

Don't do this. Since those variables are going to be used over and over, why don't you just create it once outside of loop and use it inside?

#23 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 03 December 2012 - 03:10 PM

I am a noob here. So I have a noobish question. I just received a downvote for this post. Is there a reason for this, and something I can avoid in the future? Because I don't like offending people or posting inappropriate content.

I wonder as I wander...

http://www.davesgameoflife.com


#24 lride   Members   -  Reputation: 633

Like
1Likes
Like

Posted 03 December 2012 - 03:33 PM

I am a noob here. So I have a noobish question. I just received a downvote for this post. Is there a reason for this, and something I can avoid in the future? Because I don't like offending people or posting inappropriate content.

+1 to compensate for that downvote.
It is an obvious fact that new is slow, so someone could've thought your question was ridiculous especially when you caught attention of people by capitalizing SLOW!
An invisible text.

#25 Khatharr   Crossbones+   -  Reputation: 3038

Like
0Likes
Like

Posted 03 December 2012 - 06:40 PM

I am a noob here. So I have a noobish question. I just received a downvote for this post. Is there a reason for this, and something I can avoid in the future? Because I don't like offending people or posting inappropriate content.


Some people are just in a default state of 'offended' and will apply that to whoever and whatever they come across. If your conscience is clear then you're okay.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#26 e‍dd   Members   -  Reputation: 2105

Like
1Likes
Like

Posted 03 December 2012 - 07:51 PM

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


:)

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

#27 samoth   Crossbones+   -  Reputation: 5032

Like
1Likes
Like

Posted 04 December 2012 - 07:52 AM

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, 04 December 2012 - 07:54 AM.


#28 Dragonion   Members   -  Reputation: 131

Like
1Likes
Like

Posted 04 December 2012 - 12:09 PM

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, passing, is reduced to a single ret 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 referrencing the loop is translated because two external functions (the int 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.

#29 Álvaro   Crossbones+   -  Reputation: 13905

Like
0Likes
Like

Posted 04 December 2012 - 12:21 PM

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.

#30 Dragonion   Members   -  Reputation: 131

Like
-1Likes
Like

Posted 04 December 2012 - 12:28 PM


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.


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, 05 December 2012 - 08:14 AM.


#31 iMalc   Crossbones+   -  Reputation: 2314

Like
1Likes
Like

Posted 04 December 2012 - 10:36 PM

As the assembly Dragonion posted shows:

It just goes to show that nothing is faster than something, and something is slower than nothing.Posted Image

Edited by iMalc, 04 December 2012 - 10:38 PM.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS