Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!keith From: keith@Apple.COM (Keith Rollin) Newsgroups: comp.sys.mac.programmer Subject: Re: Str255 in C Message-ID: <48119@apple.Apple.COM> Date: 15 Jan 91 21:30:14 GMT References: <1991Jan13.210327.9613@ux1.cso.uiuc.edu> <1991Jan14.142512.24220@clinet.fi> Organization: Apple Computer Inc., Cupertino, CA Lines: 35 In article <1991Jan14.142512.24220@clinet.fi> maa@clinet.fi (Miika Asunta) writes: >2. Instead of using strcopy, which is quite slow, use Toolbox routine >BlockMove, which is extremely fast: There are three flavors of copying a variable number bytes under MPW: a simple loop that copies a range a byte at a time; a smarter loop that attempts some optimization, and BlockMove(). strcpy() is in the first class. It copies bytes until it copies a 0 byte. memcpy is in the second class. It copies bytes a byte at a time until it gets to a longword aligned location, at which time it uses a ladder of MOVE.L (A1)+,(A0)+ instructions. BlockMove() is a lot fancier, faster, and also takes into account overlapping byte ranges. The point of all this is: the fancier a routine is, the higher the initial overhead. In order to be efficient, the range of bytes copied must be large enough to make the overhead small. For the simple case of copying a 10-15 byte string, strcpy() should be faster than BlockMove(). For intermediate ranges, memcpy() should be you choice. For large ranges (and for overlapping ranges), use BlockMove(). BlockMove() not only has the largest setup time (which, actually, is not all that siginificant), but also has the overhead of the trap dispatcher. I don't know what the cut-offs are, but I think I saw one routine that decided to use BlockMove() for anything over 54 bytes. On the other hand, 80%+ of the calls to BlockMove() are for 1-31 bytes, and 95%+ are for less than 256 bytes so I imagine that BlockMove() is highly optimized for those ranges. -- ------------------------------------------------------------------------------ Keith Rollin --- Apple Computer, Inc. --- Developer Technical Support INTERNET: keith@apple.com UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith "Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions