On May 10, 2009, at 1:05 AM, Gwynne Raskind wrote:

The smallest (and likely fastest) would probably be the lookup table version:
The smallest in terms of code, and it is probably the fasted method.
But it is also the most error prone  ;)


And the switch-case version compiles to three pages of assembly full of addls, addbs, hlts, jmps, and so forth.
Actually, the number of machine instructions equals 6 plus 2 times the number of case clauses.

I ran some (very) simple benchmarks (full source code at the end of this message).

...
Food for thought :).


My results:
GCC 4.0, 32-bit,  -O3:
Switch version:         0.19739 sec
Non-Standard switch:    0.184204 sec
If-else version:        0.262892 sec
Lookup version:         0.114981 sec

GCC 4.0, 32-bit,  -Os:
Switch version:         0.321028 sec
Non-Standard switch:    0.28091 sec
If-else version:        0.417788 sec
Lookup version:         0.102283 sec

GCC 4.2, 32-bit,  -O3:
Switch version:         0.269755 sec
Non-Standard switch:    0.196217 sec
If-else version:        0.270493 sec
Lookup version:         0.106891 sec

GCC 4.2, 32-bit,  -Os:
Switch version:         0.302562 sec
Non-Standard switch:    0.185923 sec
If-else version:        0.380549 sec
Lookup version:         0.098202 sec


Strict Aliasing does not have an effect in this case, so Objective-C, C and C++ should perform equally (and it did).

Test code is basically:
    volatile int c;
    clock_t t0;
    t0 = clock();
    for (int i = 0; i < 100000000; ++i)
        c = hexDigitToInt1(i & 255);
fprintf(stdout, "hexDigitToInt1: elapsed time: %g sec\n", (double) (clock() - t0) / CLOCKS_PER_SEC);


I guess in Gwynne's test code the modulo operator has a very high impact.



Regards
Andreas



Results:


GCC 4.0, 32-bit, -O3:
If-else version: 0.64217
Switch version: 0.60569
Lookup version: 0.41606

GCC 4.0, 32-bit, -Os:
If-else version: 0.70390
Switch version: 0.78775
Lookup version: 0.36639



GCC 4.2, 32-bit, -O3:
If-else version: 0.54521
Switch version: 0.59065
Lookup version: 0.42472

GCC 4.2, 32-bit, -Os:
If-else version: 0.71297
Switch version: 0.71112
Lookup version: 0.40114

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to