Archived

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

OpenGL_Guru

Assembly : When is it worth your time?

Recommended Posts

i have been reading many threads lately and assembly just was brought up in a previous thread that i started. since i dont program in assembly, only when i was in college, i was wondering who uses assembly and you if you do, is it really worth your time. i guess the next question is -- is when will it NOT be worth your time? we are looking at projected 5 - 7 Ghz machines in the next few years and about 10 Ghz machines not too long after that. yes with assembly you dont have to worry about buses and transferring data back and forth but when the CPU gets this fast is it really worth your while to save a few CPU cycles? i dont mean to rant against assembly , its lightning fast but was sincerely wondering when it will totally be useless or do you see it as something we will always need?(at a development level)

Share this post


Link to post
Share on other sites
Assembly is only worth it to access features of a cpu that your compiler dont currently support (SEE2 for example) or when you have a bottleneck and have done just about everything else possible. Mainly because unless you are an expert you will most likely write worse assembly code than the compiler would''ve done anyway.

Share this post


Link to post
Share on other sites
OpenGL_Guru, you forgot to consider that as CPU''s become faster and faster, games will also start using new computation heavy features that were not possible before thus the games will also become more CPU power demanding.

So bascially you will be using assembly to optimize parts of those new computation heavy features so they can fit the power of the new CPU''s.

Hell I am not an expert. We''ll just wait and see.

Share this post


Link to post
Share on other sites
For modern GPUs with their complex architecture, deep pipelines, branch prediction, conditional execution, parallelism etc. it is very hard to hand-code better assembly code than a good optimizing compiler will produce, whos optimizer has been built by people doing nothing but examine the target CPUs workings and squeeze every ns from the resulting code, and who have a lot of theory and tools at hand for this.

A simple example: On a modern CPU, it can be advantageous to chose a slower instruction for a certain operation to allow for parallelizing some code and avoid pipeline stalls. Few people have such intimate knowledge of the CPU''s they''re coding for workings.

I had been hand-coding critical parts of my applications in assembly language myself and had given up years ago when what the compiler produced was about 10% faster than my code ...

Share this post


Link to post
Share on other sites
yes but i think CPUs are growing at a bigger rate than the current games that are available. take a look at doom 3. whats the system requirements are for maximum performance--about 1.8 Ghz maybe less with a few things tweaked down. the minimum requirements are about 1 Ghz but i am sure this is at the lowest settings. so for a game that isnt even out yet 1.8 Ghz and 256 - 512 Mb RAM - depending on OS hunger, and we are sitting at 3.4 Ghz now, i myself have a 3.2 and 2 GB of RAM. and remember games like DOom 3 and Half Life 2 are the newest and brightest games -- not every game will require this much power.. and again they arent even out yet. right now i guess my point is is that the processor speed is getting ahead of even the latest games out there. maybe this will change i dunno.

--and you are right -- GPU's are getting more sophisticated, taking even more off the CPU than ever before and will continue to do so.. so the basis of this thread ill ask the question again --

when will it NOT be worth anyone's time to even fathom of doing any assembly code? is the time drawing nigh?

[edited by - opengl_guru on May 26, 2004 9:43:08 AM]

Share this post


Link to post
Share on other sites
I think it will always be worth when it comes to CPU instructions that might be compiled in a less effective (fast) way than you can. I don't see rapidly increasing CPU speeds as a way to make real-time programming easier, but only to make it look better and make physics and such more realistic. I think highly ajustable quality options are the way to go, this way you can get the most out of the fast CPUs and still run the game/program without needing one.
I think whenever you have the spare time and it might speed up even the least bit ASM is worth your time.
EDIT:typo

[edited by - Tree Penguin on May 26, 2004 10:37:50 AM]

Share this post


Link to post
Share on other sites
It''s worth your time, when you are very good at it and writing a operating system.

I would never touch it when it comes time to make a application/game.

Share this post


Link to post
Share on other sites
Sorry but I will have to disagree with some of what has been said in this thread. There is no computer currently available to the public right now that could POSSIBLY run Sim City 4 on max settings in a large city But yes unless the assembly code is for some extremely esoteric topic, leave it to the compiler.

Share this post


Link to post
Share on other sites
ASM is going to die soon for me.
Most of the time, the same can be done with compiler intrinsics which are now pretty fine (sometimes the difference is less than 5%) and have a lot of potential on scheduling (compiler have an high-level view of the asm code so can reshuffle) and portability (compiler may decide to use xmm8-15 on x86-64 if you''re targeting it).
As an additional plus, looks like MSVC.net 2003 does not support ASM for 64-bit targets (the arguments being that thing said above).
This is to tell you that no one should really think about learning ASM.
Not to mention the fact GCC and MSVC have very different conventions for that... porting the code is a pain.

Sure there''s still need for asm. Most OS writers will need it forever. CPUID is a nice asm instruction, just as RDTSC is... I''ll check if MSCV.net 2003 supports intrinsics for that.

By the way, ASM is getting dropped even for GPUs as everyone knows.

Share this post


Link to post
Share on other sites
I only use assembler when it''s needed.

You use assembler when building a compiler.

I never really managed to get my hand rolled SSE code to run much better than compiled code in C++, it is very tricky at best. I have to read the Intel manual sometime and find out if there is any useful info on it to help.

Peace

Share this post


Link to post
Share on other sites
quote:
Original post by Raduprv
I would really disagree that a compiler can produce equal or better ASM code, compared to a good ASM programmer.

I wonder whether you can judge that. I doubt it.

Share this post


Link to post
Share on other sites
Of course I can. I made tests.
A well written ASM program can be even one order of magnitude faster than the equivalent C program. But usually ASM it''s ''only'' twice as fast. Of course, for an OpenGL program it won''t really matter, since most of the CPU time is spent waiting for the GPU, or waiting for the PCI/AGP bus.
But for a server the difference is clearly noticeable.

Share this post


Link to post
Share on other sites
quote:
Original post by Raduprv
Of course I can. I made tests.
A well written ASM program can be even one order of magnitude faster than the equivalent C program.


You want to say that a good asm programmer can get a code 10 times faster than a c compiler can produce? If that would be possible, everyone would go back to assembly!

Why assembly can be faster manually programmed is because of that the programmer knows the problem and can optimize for the problem. But if he would know c as good as he must know assembly to optimize (-> the "equivalent" c program), the difference would shrink.

And - of course: use a good compiler!

Greetings, Jake


x = rx ( 1 - x ) ; r > 3.5699
... and chaos may begin ...

Share this post


Link to post
Share on other sites
quote:
Original post by Raduprv
Of course I can. I made tests.
A well written ASM program can be even one order of magnitude faster than the equivalent C program. But usually ASM it''s ''only'' twice as fast.


You do know that your compiler produces ASM better then most people who know asm, right? Please read karx11erx''s first post.

"Of course I can. I made tests." tests on what? sorry to tell you this, but asm is NOT uber fast compared to C/C++.

Use asm only when needed, otherwise USE YOUR COMPILER, thats what its there for!

Share this post


Link to post
Share on other sites
I''d be curious to see your tests, any proof to show ? Not to say assembler cannot go faster than a compiler, for sure, but i would expect differences in the order of 10-25% max, not 100-1000%!

If you really got a 10x difference, it''s more likely that your two programs were functionnaly not equivalent.

Y.

Share this post


Link to post
Share on other sites
It''s being told, that computer will never be so good as human programmer. Means - the compiler can''t produce better code than good assembly programmer. However - I don''t think many noobs would like to learn assembly nowadays. And, also, the big game companies don''t want to spend their programmer''s time to write the code in assembly (programming in assembly is a lot slower - no doubt).

I think, it is good to know the assembly, because you learn how the computer really works - at least it is interesting. So - if you have the time - learn it. If you just want to write fast code - learn how to use the compiler switches =).

Oxyd

- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto

Share this post


Link to post
Share on other sites
quote:
Original post by Oxyd Means - the compiler can''t produce better code than good assembly programmer.

That may be true a few years ago, but with genetic compilers; i think that commment is going to be false very soon.

Learn C++. its hard, but it works.

Share this post


Link to post
Share on other sites
Well, for example, Small, a nice scripting language we use.
It has a few implementations of the VM.
The ASM version is ~5 times faster than the ANSI C version.
But anyway, if you don''t believe me, have fun not using any ASM, and wondering why other''s people code works better.
Also, look on Quake 1 sourcecode documentation. The 3D engine (some of it) has a C and an ASM implementation. The author claims that the ASM version is twice as fast. But of course, you know better.

Share this post


Link to post
Share on other sites
Properly written assembler can be faster than C code for one single reason. A human can optimize much father ahead than a compiler. I was once writing a software renderer, only for points, as a test to see which worked faster, inline assembler or C. I was using Visual Studio 6, with full optimization enabled when doing this, and the assembler version was 2-3x faster than the C version. Simply because i could leave values on the FP stack and other places for use many lines later in the code. Aside from that assembler is also useful in using CPU features that the compiler won''t use.

Assembler is also useful for doing things that would be otherwise impossible for a C program.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I am damn sick of this argument, it crops up on every programming board every couple of weeks.

Asm IS faster than C, and is certainly faster than C++ (all those classes and templates do add overhead you know).

Asm IS needed to do things that can''t be done with C/C++. Your compiler will not do SSE, SSE2 or 3DNOW! for you, at least not at the same level as hand coded assembly language. When you speed-optimise in asm, you make significant algorithm changes for extra speed, like working on multiple vectors at once with a vector X matrix function. A compiler cannot make those changes for you.

If don''t want to learn asm, fine. But don''t put down people who have invested the time to learn asm to speed up their programs.

Share this post


Link to post
Share on other sites
Any one that is not using ASM as optimizer or even worse using a high level lanuge like Phyton is wasting time in Game Development.
No game coud posbly be writen in somthing like C# and look nice + go fast + be complex (on PC 1800/2000 mhz wich is normal today). If you dont trust me try running DX9 SDK samples for C++ & C# and then see witch goes faster.

Share this post


Link to post
Share on other sites
The only time that I have had to use assember is when coding MMX/SSE stuff. While some compilers have intrinsics now, it seems to be a very complex problem to "detect" loops and such that can be vectorized with SIMD, although that is partially due to the severe limits currently placed on SSE instructions etc.

That said, I''ve managed to get a 4-8x speed improvement on software alpha blending/mapping (it can easily get >200fps blending a full 1024x768 screen in 32-bit color... quite usable), and a 2x improvement of certain per-pixel filters (water in this case).

Then again, programming for that level of SSE is not trivial... it took me a few months to get good at. Unless you''re doing obviously vectorizable code, I wouldn''t suggest touching assembly at all. Even if you do, only convert the small vectorized portion to ASM

Share this post


Link to post
Share on other sites