ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
An enormously repetitive routine calls for simple assembler coding to better control optimization and minimize run time. Given a parent C program with a defined array1:
Code:
char array1[81];
Please look at these subroutine assembler code snippets to address, say, the element at offset 24 of the array. Which code will execute faster (fewer clock ticks),
This:
Code:
movl $array1, %edi // Outside loop, so disregard overhead
.
.
some_loop:
movb 24(%edi),%al // These instructions repeat within heavy loop
.
.
cmpb %al,24(%edi)
.
.
movb %al,24(%edi)
The target processor is germane - the timing will change across processor models. In addition, the timing on a given processor will change based on the state of the cache and the alignment of the assembly (not assembler) instructions and data.
The higher the alignment value, the more universal the benefit, and the greater the wasted space. Cache lines vary between 32 and 128 bytes on most processors.
Unless you are running your code in a single process OS and/or on a dedicated CPU, your CPU cache will be invalidated and flushed quite a bit. Optimizing your locality of reference will minimize that.
None of the performance concerns require the use of assembly code; C compilers are quite capable of performing the necessary optimizations, and allow for increased portability.
Intel has a considerable amount of information online on the subject. You can start here. This is another good article.
Last edited by macemoneta; 01-29-2007 at 10:16 AM.
Thanks for the info, and the links. I ended up doing my own benchmark tests, since i thought my endeavor may be a bit out of the ordinary, and here's what i found:
Due to several factors - the efficient use of registers and boolean logic, and creative loop controls and stack use, the difference between my assembly code and the best i could get from the C compiler was not just percentage points better, but several times faster.
As i suspected, the cache wasn't an issue. At least, running at runlevel 1 was no faster than at runlevel 2 or higher, providing i wasn't actually running something else simultaneously. I think this was due to the small size of my data and code. Align 4 was adequate. In fact, i experienced a significant speed penalty for going to align 32 or higher, probably because then my small array became big enough to require multiple cache pages.
The answer to my original question (if anyone is interested) is that the use of the data registers edi and esi to access data was faster than direct addressing by about 10%
A direct-address instruction must be fetched and decoded, then loaded into an internal micro-register to be used in the fetch. But if the address is already in one of the main segment/offset registers, it's already prepared.
There is always a tradeoff of "speed vs. space." So yes, you might well find that it will save time to store each byte of the value in a integer or long-integer. Takes eight times the storage but who cares.
Actually, i was arguing that, in this case, i got both speed and size benefits by using align 4 rather than align 32 or 64 or 128. I'm <em>guessing</em> that's because the larger data array required multiple cache pages, thus slowing things down as well as taking more space in memory. Does this run totally against the grain of processor design principles? (Remember that i'm doing this on an older chip - a 500 mHz AMD.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.