• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Hex to NSString or NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hex to NSString or NSData


  • Subject: Re: Hex to NSString or NSData
  • From: Andreas Grosam <email@hidden>
  • Date: Sun, 10 May 2009 19:45:05 +0200

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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Hex to NSString or NSData
      • From: Alastair Houghton <email@hidden>
References: 
 >Hex to NSString or NSData (From: "Mr. Gecko" <email@hidden>)
 >Re: Hex to NSString or NSData (From: Jerry Krinock <email@hidden>)
 >Re: Hex to NSString or NSData (From: "Mr. Gecko" <email@hidden>)
 >Re: Hex to NSString or NSData (From: Gwynne Raskind <email@hidden>)
 >Re: Hex to NSString or NSData (From: Andreas Grosam <email@hidden>)
 >Re: Hex to NSString or NSData (From: Marcel Weiher <email@hidden>)
 >Re: Hex to NSString or NSData (From: Gwynne Raskind <email@hidden>)

  • Prev by Date: Re: Hex to NSString or NSData
  • Next by Date: NSTask temrinationStatus always == 1?
  • Previous by thread: Re: Hex to NSString or NSData
  • Next by thread: Re: Hex to NSString or NSData
  • Index(es):
    • Date
    • Thread