Advertising (This ad goes away for registered users. You can Login or Register)

Which is faster/more efficient?

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Which is faster/more efficient?

Post by Nickolas » Tue Oct 04, 2011 4:40 pm

Is assigning a value to a variable using another variable faster than the normal way? I mean is this:

Code: Select all

const a = 1;
int b = a;
faster than this:

Code: Select all

int b = 1;
?

P.S. I only care about using the variable. The "loading" of the constant isn't my concern. I probably didn't express this very well. I mean that I don't care about the time that "loading" the constant takes.

P.S. 2 It's C# but I believe it is the exact same code in C/C++.
Advertising
Image
Image
Image
Image

User avatar
Xian Nox
Retired Mod
Posts: 2749
Joined: Fri Nov 05, 2010 5:27 pm
Location: Over the hills and far away

Re: Which is faster/more efficient?

Post by Xian Nox » Tue Oct 04, 2011 4:51 pm

The second one I think. And anyway, the speed difference is so low (ns), that you won't notice it.
Advertising

User avatar
codestation
Big Beholder
Posts: 1660
Joined: Wed Jan 19, 2011 3:45 pm
Location: /dev/negi

Re: Which is faster/more efficient?

Post by codestation » Tue Oct 04, 2011 4:55 pm

You have 4 answers for that:

1) The second one is more efficient because the first one involves 2 memory accesses and the other one doesn't since the constant value is probably embedded in the asm opcode (this is dependent of the target architecture).

2) Your compiler will optimize that code to your 2nd case so it doesn't matter.

3) It depends on the language so C#, C and C++ compilers can have different output for the same code depending on the type and optimization levels.

4) Why do you care for that? the compiler knows better than you. Also don't optimize prematurely, you can lose precious time on things that the compiler will be doing anyway. If you feel that this piece of code is the bottleneck of your app then use a profiler to find it out.

P.S.: i'll go with the 4th.
Last edited by codestation on Tue Oct 04, 2011 4:58 pm, edited 1 time in total.
Reason: typo
Plugin list
Working on: QPSNProxy, QCMA - Open source content manager for the PS Vita
Playing: Error: ENOTIME
Repositories: github, google code
Just feel the code..

Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Re: Which is faster/more efficient?

Post by Nickolas » Tue Oct 04, 2011 5:04 pm

I asked just out of curiosity but this could really affect large scale programs... (Maybe games...) Thanks for answering though! :D
Image
Image
Image
Image

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Which is faster/more efficient?

Post by m0skit0 » Wed Oct 05, 2011 8:56 am

Nickolas wrote:this could really affect large scale programs...
No. Read codestation's 4th answer again ;)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Re: Which is faster/more efficient?

Post by Nickolas » Wed Oct 05, 2011 10:25 am

m0skit0 wrote:
Nickolas wrote:this could really affect large scale programs...
No. Read codestation's 4th answer again ;)
Oops. Must have missed that. :oops: Anyway, thanks for answering.
Image
Image
Image
Image

Post Reply

Return to “Programming”