Page 1 of 1
Why computer programs are slow
Posted: Sun Apr 17, 2005 12:27 pm
by Colonel Kernel
srg wrote:BTW I'd rather program in asm than .net personally, IMHO moving away from native code programs for general use is a bad idea and will lead to slower computers guzzling more and more processing power and memory. Fine for prototyping and small programs, but not large applications.
FWIW, stupid design can do way more harm to an app's performance than the underlying run-time environment. I've seen apps in "native" C and C++, in .NET, and in Java that are all equally slow and memory-hungry (rule #1 -- plain old data should
not be modeled as objects... but that's a rant for another time). The poorly-designed stuff is not the work of amateurs either -- and this is stuff that is sold for ridiculous sums of money. Sometimes working in this industry makes me kinda nauseous.
That said, I don't see much point in using asm for application programming anymore. If anyone doubts this, try working over 100,000 lines of enterprise software with a profiler and you can tell me whether hand-writing bits of it in assembler will do
any good whatsoever.
In my experience, it's misused algorithms, naive mis-application of OO design, thrashing of the heap (too many new's and delete's in C++), inappropriate use of threads (having them wait for each other unnecessarily, or doing background tasks on a UI thread), and long network delays that make the performance of many apps suffer. Save the asm for what it's for -- OS/driver/firmware dev., and optimization of really performance-critical CPU-intensive code (game engines, whatever).
End of rant, back on topic...

I use nasm too, but since I want my OS to be portable, I'll probably learn gas as soon as I have the time.
Re:Why computer programs are slow
Posted: Sun Apr 17, 2005 12:47 pm
by Candy
Colonel Kernel wrote:
In my experience, it's misused algorithms, naive mis-application of OO design, thrashing of the heap (too many new's and delete's in C++), inappropriate use of threads (having them wait for each other unnecessarily, or doing background tasks on a UI thread), and long network delays that make the performance of many apps suffer. Save the asm for what it's for -- OS/driver/firmware dev., and optimization of really performance-critical CPU-intensive code (game engines, whatever).
In my experience it's (on a low level) indirectly doing work more than once and doing some things over and over again without any reason. Since CPU's never say they're bored with what they're doing they are usually instructed to calculate some things hundreds of times or more, for each time X is done. Also, some people seem to do calculations on everything first and then do a selection of it.
Re:Why computer programs are slow
Posted: Sun Apr 17, 2005 1:09 pm
by IRBMe
It's a well known phenomenon that only about 90% of the running time of an application is spent in executing only 10% of the code. If you were to double the speed of all your code, you would increase the speed of your app by 50%. If you were to double the speed of just that critical 10% then you would gain roughly a 40% speed up. Not bad considering you only did 1/10 of the work required to optimise the entire code.
Generally what makes applications slow is sloppy code (i.e. repeating calculations unecessarily, unecessary condition checking and so on) and bad algorithm design. If you want to sort 10 million items very quickly, a bubble sort isn't going to be a good idea but a heap sort or a quick sort will definetly be a better idea.
The choice of langauge really doesn't make that much of a difference. It's quite possible to make a responsive, fast program in java, and write the same program in C and have it slow and memory consuming.
Re:Why computer programs are slow
Posted: Sun Apr 17, 2005 1:49 pm
by Colonel Kernel
Now that this has its own thread

I can expand a bit further on my original points...
In my experience it's (on a low level) indirectly doing work more than once and doing some things over and over again without any reason.
Generally what makes applications slow is sloppy code (i.e. repeating calculations unecessarily, unecessary condition checking and so on) and bad algorithm design.
Very true, but what's really interesting is to look at the reasons why such redundancies show up in code. From what I've seen, the reason is often poor interface design rather than basic incompetance on the part of a single developer.
For example, let's say developer A writes a library, and developer B (probably working for a different organization) writes an app using that library. Let's say the library does some calculations on behalf of the app, but actually does more work than necessary and provides no means of disabling that feature. Now developer B has to write almost the same code as developer A to do a slight variation of the original work,
and they both end up getting executed and the library's results simply thrown away.
I've seen this kind of thing happen all the time. IMO, it's because interface design is hard, requirements analysis is hard, and most software is developed in a hurry, so 3rd party components get used when perhaps they shouldn't.
Re:Why computer programs are slow
Posted: Sun Apr 17, 2005 3:37 pm
by Curufir
Of course another reason is poor matching of algorithms to specific problems.
IRBMe wrote:
If you want to sort 10 million items very quickly, a bubble sort isn't going to be a good idea but a heap sort or a quick sort will definetly be a better idea.
Good example. On the flip-side bubble sort will outperform heap/quick on very small sets. Same problem, different criteria, therefore different algorithm required for optimal sorting. I'm sure there are plenty of people out there that just keep throwing sets at quicksort library routines no matter how large the set is.
Re:Why computer programs are slow
Posted: Sun Apr 17, 2005 9:18 pm
by B.E
Colonel Kernel wrote:
That said, I don't see much point in using asm for application programming anymore. If anyone doubts this, try working over 100,000 lines of enterprise software with a profiler and you can tell me whether hand-writing bits of it in assembler will do any good whatsoever.
I agree with you about programming enterprise software entirely of assembly is stupid. But I don't think that assembly should be forgotten completely.
Although it is no longer neccssary for programmers to write programs in machine code, an understanding of the principles in program operation at machine level assists programmers in understanding why certain features of some languages should be used or avoided in the interest of efficieny of execution. SUch an understanding also helps to remove the mystique surrounding programs and exposes them as collection of numbers which have significances when unterpreted by the CPU
THe examination of program operation at this level requires the programmer to be on much more familiar terms with the working of a computer than is neccessary when using a high-level language.
Re:Why computer programs are slow
Posted: Sun Apr 17, 2005 11:28 pm
by Colonel Kernel
B.E wrote:THe examination of program operation at this level requires the programmer to be on much more familiar terms with the working of a computer than is neccessary when using a high-level language.
I completely agree. This is why I think anyone entering the field should spend some time doing OSdev to get a full appreciation for how the machine works.
Even better -- spend some time doing distributed programming so you get a sense of the performance characteristics of an entire network of machines.
Re:Why computer programs are slow
Posted: Mon Apr 18, 2005 2:26 pm
by DruG5t0r3
Programming for dummies..."why programs are slow"
Code: Select all
#define ONEBILLIONTRILLION 1000000000000000....
int void main() {
for (int i=0;i <= ONEBILLIONTRILLION;i++) { donothing(); }
printf("HellllOOO world!\n");
return 0;
}
Re:Why computer programs are slow
Posted: Tue Apr 19, 2005 12:22 am
by distantvoices
Sometimes, it suffices for cpu hogs (polling on a file semaphor in order to wait for the compiler to finish his work on the host - this would be for an UTM-transaction monitor thing coupled with project deployment) to simply YIELD() their timeslice after having performed their poll. It's simply more polite to all the others and increases overall speed and performance of the system.
But tell this an old asm programmer who has only ever done coding on a mainframe:
Yield()? Thats something from PC, that doesn't exist on the mainframe (nonsense in my not so humble opinion). Oh, but then, after some slight argument, that programmer would admit "There is a way to interact with the system via assembly calls but I dunno where"
Just think about it if you need to do busy waiting: yield() can increase the overall performance and the cpu hog won't get a penalty. +gg* It's just that not all programmers *care* or *know* about OS friendly coding style but only about doing their application and that's it.
Re:Why computer programs are slow
Posted: Tue Apr 19, 2005 1:23 am
by Pype.Clicker
not mentionning graduated CS engineers that do O(N^3) nested loops comparing strings to discover matching items in a language that natively support efficient hash functions.
or people that don't even worry about their C++/Java strings receiving one new character at a time (and don't even know about the existence of StringBuffer

)
I guess you got my point: programs are slow because poorly written. And programs are poorly written because environments less and less focus at making efficient programs elegant. They instead focus at making time-to-market faster :'(
Re:Why computer programs are slow
Posted: Tue Apr 19, 2005 1:54 am
by distantvoices
Those graduated cs engineers should go work for some time. Wanna see their bosses look at that "but so cool" solution, which takes time time time - inefficient so to say and tell them: "Hey, man, you tell me you can do good code? 't is crap, nuttin more, man! Wake up! 't ain't work"
BTW, I'd like to know about those hash solutions. It might speed up several pattern recognition ideas I need to implement in order to get a kind of interpreted language implemented for BlueIllusion Shell.
Re:Why computer programs are slow
Posted: Tue Apr 19, 2005 3:41 am
by Brendan
Hi,
There are other reasons programs are slow...
Of these I'd include multi-media and GUIs as the largest. A text editor operating on ASCII in 80*25 text mode (e.g. DOS "edit") is far more efficient than a text editor operating on unicode in 1024 * 768. Most modern software is expected to have a full graphical interface with icons, some animations, variable sized unicode fonts, scrolling sub-windows, etc. It all looks pretty but there's a price..
Multi-processing and multi-tasking also adds to the overhead, especially when work is being done in the background without the user realizing. This is influenced a fair bit by the scheduler design, but even in the minimal case you've still (typically) got an IRQ interrupting the CPU to work out which task should be doing what.
The next is "protection". IMHO the more protection there is the slower things get. For example, my OS protects IO ports and emulates IN/OUT within the GPF handler (I could use the IO permisson bitmap in the TSS, but either way it adds overhead). In general any form of protection involves some method of restricting access, which involves some form of overhead.
There's also "security" - virus checking, spyware software, firewalls, etc. Most of this runs in the background taking CPU time away from what you want to do. IMHO this is the only thing I've listed that I'd attribute to poor design - the OS should prevent virii from infecting to begin with, rather than requiring software to detect them and clean up after. "Enhancements" that allow spyware should be banned, etc.
Virtual memory can also contribute - hundreds of page faults as pages are allocated one at a time rather than using a single system call to allocate a large number of pages, pages being sent to/from swap, etc. To a small degree virtual memory also effects the way people design software - when the application is faced with several GB of virtual memory to use it's easier to forget about how much physical memory you're using.
Memory mapped executables also effect performance. Once upon a time you'd put your floppy in, start the application, make a cup of coffee then return to an application that runs at 100%. Now the OS often memory maps the executable so that the same application "starts" much faster, but once it has started the remainder of the executable is being loaded while you're using the application. Rather than having a delay with an obvious cause followed by full speed operation you get a small delay with an obvious cause followed by sluggish performance that improves. When a user starts an application they expect it to be loaded from disk, but when this loading is "hidden" and occuring while they're trying to work they often don't know why, and wonder why they aren't getting 100% performance to begin with.
Then there's "feature creep". For example, the latest versions of Word for WIndows do spell checking and grammar checking while you type, which is obviously going to take more CPU time. Spreedsheets do syntax checking, compilers are getting more and more complex, web browsers are handling many more types of content. You get the idea..
Most of this can be attributed to progress, rather than poor design, poor algorithms, lack of optimization or poor languages/compilers (although they'd all effect performance too).
Cheers,
Brendan
Re:Why computer programs are slow
Posted: Tue Apr 19, 2005 4:25 am
by bubach
All kind of computers are getting faster and faster, and people care less and less about writing good code becasue of that.
If the development of faster computers stopped a long time ago, we wouldn't have this "problem"...
Just look at my brothers mobile, it's "only" a 109 Mhz but runs a Java(!) Xvid player. ;D
Re:Why computer programs are slow
Posted: Tue Apr 19, 2005 4:46 am
by Solar
Brendan wrote:
A text editor operating on ASCII in 80*25 text mode (e.g. DOS "edit") is far more efficient than a text editor operating on unicode in 1024 * 768.
Ahh... but don't you work much more efficiently with an editor that shows you tabs of the open files, a project hierarchy in a dock window on the left and the last search / compile results in the bottom, where you only have to click on an error message to open the source file at the relevant line?
Of course software "bloated". But demands on the software "bloated" too. Back then, we used pirated Koalapainter to draw funny doodles. Today, we use pirated Photoshop to edit away our mother-in-law from truecolor digital photos...
Memory mapped executables also effect performance.
Expectations outgrew memory availability. If you'd load all of your office suite into memory you'd either need multiple GB's of RAM, or you'd be hit by VM overhead just as heavily. This is here to stay; all we (OS dev'ers) could do is making it work as best we can.
Spreedsheets do syntax checking, compilers are getting more and more complex, web browsers are handling many more types of content. You get the idea..
I agree on the WP and spreadsheet. Compilers do their business
once, and I'll happily accept a performance hit during compilation if it adds to the performance to the executable (or to the convenience to the development process). As a developer, it's upon
you to take the inconvenience so that your user doesn't have to. That, too, is here to stay as users will always outnumber developers.
As for web browsers, a smart system-wide plugin content handling system should take care of that...
Most of this can be attributed to progress, rather than poor design, poor algorithms, lack of optimization or poor languages/compilers (although they'd all effect performance too).
One of the biggest problems
I see that "developing software" has grown from a geeky artform to an industrial product. Too many people are working in the field today who don't have the tiniest idea of what their work is about. They don't care for quality, don't care for their work, don't care for their competence outside of "functioning" inside their peer environment.
This applies
both to the corporate RAD drones
and major parts of the Open Source movement.