From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,baaf5f793d03d420 X-Google-Attributes: gid109fba,public X-Google-Thread: fc89c,97188312486d4578 X-Google-Attributes: gidfc89c,public X-Google-Thread: 1014db,6154de2e240de72a X-Google-Attributes: gid1014db,public X-Google-Thread: 103376,97188312486d4578 X-Google-Attributes: gid103376,public From: ohk@edeber.tfdt-o.nta.no (Ole-Hjalmar Kristensen FOU.TD/DELAB) Subject: Re: What's the best language to start with? [was: Re: Should I learn C or Pascal?] Date: 1996/08/13 Message-ID: #1/1 X-Deja-AN: 173873327 references: <31FBC584.4188@ivic.qc.ca> organization: Telenor R&D newsgroups: comp.lang.c,comp.lang.c++,comp.unix.programmer,comp.lang.ada Date: 1996-08-13T00:00:00+00:00 List-Id: I have some supporting data for your case. Consider the following rather different programs which copy data as fast as possible: :::::::::::::: duff.cc :::::::::::::: int from[1000000], to[1000000]; int main(int ac, char **av) { int count = sizeof(from) / sizeof(int); for (int i = 0; i < 100; i++) { int * s = from, *d = to; register int n = (count + 15) / 16; switch (count % 16) while (--n) { case 0: *d++ = *s++; case 15: *d++ = *s++; case 14: *d++ = *s++; case 13: *d++ = *s++; case 12: *d++ = *s++; case 11: *d++ = *s++; case 10: *d++ = *s++; case 9: *d++ = *s++; case 8: *d++ = *s++; case 7: *d++ = *s++; case 6: *d++ = *s++; case 5: *d++ = *s++; case 4: *d++ = *s++; case 3: *d++ = *s++; case 2: *d++ = *s++; case 1: *d++ = *s++; } } return 0; } :::::::::::::: loop.cc :::::::::::::: int from[1000000], to[1000000]; int main(int ac, char **av) { int count = sizeof(from) / sizeof(int); for (int i = 0; i < 100; i++) { int * s = from, *d = to; for (int j = 0; j < count; j++) { *d++ = *s++; } } return 0; } :::::::::::::: loop2.cc :::::::::::::: int from[1000000], to[1000000]; int main(int ac, char **av) { int count = sizeof(from) / sizeof(int); for (int i = 0; i < 100; i++) { for (int j = 0; j < 1000000; j++) { to[j] = from[j]; } } return 0; } :::::::::::::: rloop.cc :::::::::::::: struct buf { int i[1000000]; }; buf from; buf to; int main(int ac, char **av) { for (int i = 0; i < 100; i++) { to = from; } return 0; } :::::::::::::: mem.cc :::::::::::::: int from[1000000], to[1000000]; int main(int ac, char **av) { int count = sizeof(from); for (int i = 0; i < 100; i++) { memcpy((char *) to, (char *) from,count); } return 0; } All programs are compiled with gcc on a SPARC. No optimization: time duff 76.2 real 65.2 user 1.8 sys time loop 96.0 real 77.1 user 1.7 sys time loop2 89.6 real 72.1 user 1.9 sys time rloop 42.2 real 23.7 user 2.2 sys time mem 45.5 real 24.5 user 1.9 sys -O3: time duff 42.4 real 23.6 user 1.4 sys time loop 43.5 real 26.0 user 1.8 sys time loop2 40.9 real 24.1 user 1.8 sys time rloop 40.8 real 24.1 user 1.8 sys time mem 43.5 real 23.7 user 1.6 sys -O3 -funroll-loops: time duff 45.4 real 23.6 user 1.7 sys time loop 45.0 real 23.0 user 1.7 sys time loop2 46.0 real 23.4 user 1.9 sys time rloop 44.3 real 24.3 user 1.9 sys time mem 42.5 real 24.0 user 1.8 sys Conclusion: In this case, even manual loop unrolling does not buy you very much if you are going to compile with an optimizing compiler. (This example is probably too simple, although the first program should be obfuscated enough...) For a loop such as this, using arrays instead of pointers is marginally faster, unless I turn on the -funroll-loops flag, for some reason. It is interesting to note that using memcpy does not increase the speed of copying. This may be different on other CPU's.