flat assembler
Message board for the users of flat assembler.

Index > Main > which is faster?

Author
Thread Post new topic Reply to topic
michalll



Joined: 01 Mar 2007
Posts: 7
michalll
I was just wondering when I want to increase a value by two is it faster to use
Code:
add ax,2    
or
Code:
inc ax 
inc ax    

?
Post 29 Oct 2008, 10:01
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
While it is hard to predict exactly which of the above code sequences would be faster in all circumstances I would hazard a guess that in many situations one would find that the first example would give better results.

Although it would pay to keep in mind that trying to glean execution speed from an isolated instruction (or two) is extremely prone to errors when one takes into account that a modern x86 CPU is very complex. There can be many other things happening, from either previous or following instructions, that may cause alternative instruction(s) to be faster in some specific situations.

In short: It is impossible to know for sure but stick with the first and I think you would not be too disappointed.
Post 29 Oct 2008, 10:47
View user's profile Send private message Visit poster's website Reply with quote
Raedwulf



Joined: 13 Jul 2005
Posts: 375
Location: United Kingdom
Raedwulf
It really depends on your processor, but I agree with revolution.
Code:
inc ax
inc ax
    


means that the second inc depends on the first inc - which would mean that the processor would have to wait for the first one to complete. Of course, if the processor has some logic to detect two consecutive incs - then it would process it faster.

_________________
Raedwulf
Post 29 Oct 2008, 12:13
View user's profile Send private message MSN Messenger Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
IMHO any slight difference in speed, faster or slower, would be insignificant in 99.999% of applications because register operations like this are much faster than instructions which require memory R/W cycles and almost infinitely faster than most I/O operations, the latter two of which are actually going to determine the overall speed of your application.

True, the instructions have to be initially fetched from memory, twice in the first case, and once for the second (but also another memory access to get the immediate data '2') but pipelining and prefetching and caching and parallel operations make this argument all a bit meaningless.

_________________
FAMOS - the first memory operating system
Post 30 Oct 2008, 02:12
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.