Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ptsfa!ames!husc6!seismo!mimsy!chris From: chris@mimsy.UUCP Newsgroups: comp.lang.c Subject: Re: Copying a constant number of bytes Message-ID: <7044@mimsy.UUCP> Date: Sun, 14-Jun-87 22:29:04 EDT Article-I.D.: mimsy.7044 Posted: Sun Jun 14 22:29:04 1987 Date-Received: Tue, 16-Jun-87 01:06:47 EDT References: <900@bloom-beacon.MIT.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 36 In article <900@bloom-beacon.MIT.EDU> newman@athena.mit.edu (Ron Newman) writes: >I want to copy a constant number of bytes NBYTES from address "a" >to address "b", where "a" and "b" are pointers of unspecified type. >Which is the more efficient method for most compilers and machines >currently in use? >1) bcopy ((char *)a, (char *)b, NBYTES); >2) struct nbytes {char s[NBYTES];}; > *(struct nbytes *)b = *(struct nbytes *)a; >Method (1) has the disadvantage that it always involves a subroutine >call and return. Not necessarily: in the dpANS, some functions may be `built in' to the compiler; in 4.3BSD, you can use the `inline' program, found in /sys/vax/inline/inline, to expand certain calls (including bcopy) in line. >But can I count on a compiler generating efficient code for method (2)? Obviously this is compiler dependent. Some existing compilers do not even support structure assignment. I hope that any compiler that does support it does the copy at least as fast as via a bcopy subroutine call. >Is the answer any different if I know that "a" and "b" are 32-bit >aligned, or that NBYTES is a multiple of 4? Not if you want real portability. If this particular copy *must* be fast, I would suggest defining a macro to do it, and using whichever method is fastest on your machine. The macro can be put in a file that is clearly marked `system dependencies'. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris