Sign in to follow this  
KodeNerd

Assembly Optimization

Recommended Posts

I have been perusing around and have noticed that there are some articles on x86 which you can use to optimize C++. Is this truly necessary for the work that may go into the optimization (like how much work for how much optimization estimate)?

Share this post


Link to post
Share on other sites
Unless you're an assembly optimisation expert, don't bother. It's likely that the compiler can generate better, more optimised assembly than an amateur assembly programmer could. Modern optimising compilers do an excellent job of optimising C++ code.

I'd estimate that an expert hand-writing a piece of code in assembly would outperform a well-written C++ equivalent by about 10%. But that'd be for an expert, and certainly not for someone who's just beginning ASM.

Rewriting C++ code in ASM should really be a "last resort" plan. If, after profiling, you need more performance, you should look for high-level algorithmic optimisations first. Only if all other options have been exhausted should you resort to writing something in ASM, in my opinion.

Share this post


Link to post
Share on other sites
Writing in assembly is useful for algorithmic optimization - taking advantage of knowledge your compiler doesn't and can't have. By properly setting up the data, and processing it in data-oriented manner, you can gain significant benefits.

Another area is SIMD algorithms. Compilers in general cannot do anything useful in this area, so writing those manually is the only option. The benefits can improve algorithm running time several times.

Lastly, you need assembly if you want to interface hardware directly for whichever reason.

But for general code optimization, the trade-off between development time and maintenance is rarely worth it. Assembly is just another tool, you use it when needed.

Share this post


Link to post
Share on other sites
I only skimmed it, but this appears to be a pretty good article on the subject.

In short, learn assembly language... but don't learn it to optimize your C++ code. Learn it because it will help you understand how C++ works, which will in turn help you to write better C++ code.

And if you ever have to debug in assembly language, knowing what's going on is very very helpful.

Share this post


Link to post
Share on other sites
Yeah, what exwonder said. At this point I know a fair bit of assembly, but I've only done non-trivial assembly programming once in my career. That was because the platform I was working on had a terrible compiler, and (at the time) assembly was the only way to take full advantage of the vector-float stuff it offered. Mostly I just use that knowledge to help debug code inside libraries (where we don't have source code), or to examine generated code and figure out what hints we should be giving the compiler to help it optimize better.

The other important thing is that in many cases, unless you REALLY know what you're doing, assembly will hurt performance. Once you start putting inline assembly in your code, the C++ optimizer gets very scared, and backs off. Instead, you should be using compiler-supplied intrinsics, which let you use all of the cool instructions that the compiler might not use, but use them in a way that's more friendly to the compiler (and your brain!)

Share this post


Link to post
Share on other sites
Thanks for the article.

It is actually good because I have been using the Z80 (in most TI-83+ calculators) for a month and have noticed a change in how I think about my applications.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this