• Advertisement

Archived

This topic is now archived and is closed to further replies.

Divisions are Slower, are Ifs slower that Multiplying?

This topic is 5062 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

I''m programming in C++ @ the moment, & in ASM, i read few documents on ASM, but what I would like to know-- Does anyone know of a table or chart that has the operators & their speed. Dun mes wit me!

Share this post


Link to post
Share on other sites
Advertisement
MASM, just inline asm code with c++.

I know that divsions are slower than Multiplications, and muliplications are slower than just addition but then im using shift bitwise operators too, and I don''t know where that fits in, speed wise.

Dun mes wit me!

Share this post


Link to post
Share on other sites
Sweet as.
I assumed it was, and i just searched the net & for sure it is.

http://www.developer.com/java/j2me/print.php/10934_2234631_5

for anyone wanting that optimizing edge, check out bottom links too.

I find it funny when people try finding the average of 4 pixels by adding then dividing it by 4. Shifts r better.

Thanks!


Dun mes wit me!

Share this post


Link to post
Share on other sites
Ului, most compilers will automatically convert an integral division by 4 to a shift. As long as the denominator is a constant, there''s little reason to do it yourself.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Hmm, learn something new every day. But I usually code in asm, so i do it manually.



Dun mes wit me!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
These days its not so much what the instruction speed is (although for SOME slow instructions such as mul or div it matters) but rather what is important is what instructions it can be paired with. On modern processors you can often get two for the price of one when you structure your assembler code properly.

- Rockoon

Share this post


Link to post
Share on other sites
A mul is done in 1 cycle IIRC. It doesn''t pair well, and uses two registers, so if you can do it with a single shift it would be faster. I highly doubt that an addition loop could be faster, I suspect it would be many times slower.

Share this post


Link to post
Share on other sites
On modern CPUs it is never as simple as "this instruction takes n cycles"

Because they tend to try and execute instructions in parallel, instruction order makes a difference as well as the instructions themselves. Also loads can take a lot longer depending on caches etc; branching can change.

I would imagine that an optimising compiler could change a constant divide by 4 into a multiply by 0.25.

Anyway if you are doing some scientific computing where you need to worry about this kind of thing, you really need a different forum.

Consider buying different hardware which is better at the operations you need a lot of (obviously profile your code a lot first)

Mark

Share this post


Link to post
Share on other sites
quote:
Original post by Ului
Sweet as.
I assumed it was, and i just searched the net & for sure it is.

http://www.developer.com/java/j2me/print.php/10934_2234631_5

for anyone wanting that optimizing edge, check out bottom links too.

I find it funny when people try finding the average of 4 pixels by adding then dividing it by 4. Shifts r better.



I think the first three points in that link are worth repeating
quote:

Only optimize code if you need to
Only optimize where it counts
Use the profiler to see where to optimize



I find it funny when people code in asm when HLLs exist. I assume there''s a good reason for it, otherwise you fail guidelines 1 and 2.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Ului
I find it funny when people try finding the average of 4 pixels by adding then dividing it by 4. Shifts r better.


Me too, especially as they''ve just added 4 pixel values together and then divided by 16 ... thus illustrating the dangers of the obsfutication when trying to optimise!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
> I find it funny when people code in asm when HLLs exist. I assume there''s a good reason for it, otherwise you fail guidelines 1 and 2.

There are times when you are certain you will be hand optimising something later on. In these cases it is important to be mindful that certain algorithms when expressed in assembler are much better than others, even if the algorithmic complexity (the big-O notation) appears to be "worse". A certain string searching algorithm comes to mind...

- Rockoon

Share this post


Link to post
Share on other sites
btw what is HLLS? also the profiler u mentioned? I''m self taught so there were bound to be things I missed.

Dun mes wit me!

Share this post


Link to post
Share on other sites
HLLs = High-level languages
Profiler = A tool/class/whatever, that measures the time spent in each function and then gives you a chart or something simmiliar where you can see what to optimize first.

Share this post


Link to post
Share on other sites
Though a hardcore asm programmer myself, I must admit that writting a large piece of code in asm is a nightmare ( even with years of experience) and gain is minimal or none, sometimes it can happen that it's even more expensive than written in HLL. Anyway asm instruction performance before P3's could be simply measured in clocks, since the parallelization was still poor and just a handful of instructions could run in parallel and not in all orders. But with introduction of P3's you need to take into consideration 5 levels of flops and their execution costs, how each instruction pairs and in what pipeline (looping access is still an issue even in P4's as I gather from the docs), all different types of stalls, especially ones incurred by integer/floating switching. So if you want to optimize you see there's a sea of opportunities, which compilers are more accustomed to than we can so it's a task better left to them. BUT if optimal code is wanted you SHOULD write in asm small pieces and routines which are identified as bottlenecks (by a profiler or any other way) and/or are called many times. Compilers still operate on micro levels (ie. they can optimize code in one segment since most of the optimization opportunities occur locally). On the other hand hand optimizing is best done on longer routines or specific routines where locality of code is only known to you.

Now I'm going to shut up, cause I talked to much!

[edited by - weddo on April 12, 2004 9:07:26 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by sjelkjd
quote:
Original post by Ului
Sweet as.
I assumed it was, and i just searched the net & for sure it is.

http://www.developer.com/java/j2me/print.php/10934_2234631_5

for anyone wanting that optimizing edge, check out bottom links too.


I think the first three points in that link are worth repeating
quote:

Only optimize code if you need to
Only optimize where it counts
Use the profiler to see where to optimize


I find it funny when people code in asm when HLLs exist. I assume there''s a good reason for it, otherwise you fail guidelines 1 and 2.


Wasn''t that link about Java Micro edition? Basically handhelds and mobile phones.


Yes there are a lot of nice reasons for it. You need talk to the hardware directly (And don''t like the idea of a buggy template). You need to do something with the CPU. You need to create a very nice library that would work on PII. You need to call system services. BTW macro assembly is nicer than C++, so there are situations where it''s usefull.

BTW when you are writing something in the assembly, you create documentation as well, and you are trying to have that code well structured. (On other hand Java or C# code could be mess becose programmers have no need to be carefull. C++ is mess nearly always.)

Share this post


Link to post
Share on other sites
I think modern compilers optimize things much more better than humans do, isn''t it? maybe using mmx, sse and things like that are a good bet, but i think that trying to optimize such small things is (sorry for this) stupid.

Share this post


Link to post
Share on other sites
It depends. Compilers aren't omniscient, and for example the MSVC is optimizing very locally.

[edited by - raghar on April 12, 2004 10:26:30 AM]

Share this post


Link to post
Share on other sites
Raghar:Well, if you can check the global-optimization-option and choose the other optimization options carefully the compiler will do magic...

Share this post


Link to post
Share on other sites
Teh problem is, you could write it in assembly faster than check all necessary switches in compiler, and verify if compiler didn''t do something stupid.

Share this post


Link to post
Share on other sites
quote:
Original post by Raghar Wasn''t that link about Java Micro edition? Basically handhelds and mobile phones.


Yes, and notice how the first guideline was still "only optimize if you need to."

quote:

Yes there are a lot of nice reasons for it. You need talk to the hardware directly (And don''t like the idea of a buggy template). You need to do something with the CPU. You need to create a very nice library that would work on PII. You need to call system services. BTW macro assembly is nicer than C++, so there are situations where it''s usefull.


Sorry, I disagree. Yes, assembly has its place. That place is 1) if you need hardware access that the programming language doesn''t supply, or 2) for optimization after you''ve profiled, or 3) you are writing a high performance library(we''re talking something that doom3/hl2 might use) and you can reasonably predict bottlenecks because you have a lot of experience.

quote:

BTW when you are writing something in the assembly, you create documentation as well, and you are trying to have that code well structured.


You do this in high level languages as well. Code tends to be better documented and better structured in high level languages than in asm.

quote:

(On other hand Java or C# code could be mess becose programmers have no need to be carefull. C++ is mess nearly always.)


Given any coder who knows both java/c#/c++ and asm, I assert that the java/c#/c++ code will almost always be less messy than
the assembly. Assembly does not allow you to make meaningful abstractions.

To the OP: unless you have a really valid reason for it(like you''re writing OS routines, or microcontroller code), you''re probably better off concentrating on c++ and forgetting about asm.

Share this post


Link to post
Share on other sites
quote:
Original post by Raghar
Teh problem is, you could write it in assembly faster than check all necessary switches in compiler, and verify if compiler didn''t do something stupid.


you''re joking, right?

Share this post


Link to post
Share on other sites
Although I write most of my code in HLLs, knowing Assembly helped me a lot in understanding of what goes on in the "background".

Even if a programmer never uses it, I think he should at least be able to READ assembly code.

Just had to add my 2 cents


Indeterminatus

--si tacuisses, philosophus mansisses--

Share this post


Link to post
Share on other sites
Just answer the topic, hope that still help:

http://packetstormsecurity.nl/programming-tutorials/Assembly/fpuopcode.html

!o)

Share this post


Link to post
Share on other sites

  • Advertisement