TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CAREY BLOODWORTH
from: HERMAN SCHONFELD
date: 1997-04-24 18:02:00
subject: Low level optimizatio 1/2

CB>Let's work through a simple project.  Let's say that we need to copy
CB>some data from one array to another.  Basically, just like memcpy()
I don't need to be tutured!
CB>void memcpy(char to[], char from[], int len)
CB>{ while (len--) to[len]=from[len];}
Sloppy code!
And you claim that optimization creates sloppy code.
First off all, try unrolling the loop, and use pointer refferencing.
But according to you, unrolling is bad, then use Duffs Device.
CB>void memcpy(char *to, char *from,int len)
CB>{ while (len--) *to++=*from++;}
len needs not be used like that in a while statement like that.
It uses a register (which could be used for something else) so remove it, and 
restructure your loop.
If you don't know how, then message me back!
CB>That works well and is reliable.  We can optimize that a bit by
CB>realizing we are only doing a char at a time, and that we could move
CB>words at a time.  So, we'd have:
CB>void memcpy(char *to, char *from,int len)
CB>{int *t=(int*)to;
CB> int *f=(int*)from;
CB> len/=sizeof(int);
CB> while (len--) *t++=*f++;
CB>}
What do you think i am? Some kind of incompetant retard?
Thank you for your free lessons, but I don't need them.
... Energizer Bunny Arrested! Charged with battery.
--- Ezycom V1.48g0 01fd016b
---------------
* Origin: Fox's Lair BBS Bris Aus +61-7-38033908 V34+ Node 2 (3:640/238)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.