 |
 |
Some interesting figures.
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
I was curious about the relative speeds of C, C++ and Objective-C at common real-world tasks, so I wrote a little program to test their performance with arrays. In each case, the most 'native' form of array was used - so we have an int[], a vector<int> and an NSArray of NSNumbers. Each language was called upon to create an array, fill it with numbers from 1 to 100, reverse it and then destroy it. Thanks to the wonders of Objective-C++, everything was all in a single source file, too! Some day I'll learn PPC assembler and add that:)
Code:
[untermac:Current/efficiencyComparison/build] banana% ./efficiencyComparison
cInitArray:4504 cFillArray:33 cReverseArray:38 cDestroyArray:126 Total: 1927426
cInitArray:79 cFillArray:51 cReverseArray:43 cDestroyArray:146 Total: 1129196
cInitArray:54 cFillArray:22 cReverseArray:27 cDestroyArray:62 Total: 13885
cInitArray:32 cFillArray:22 cReverseArray:28 cDestroyArray:56 Total: 13827
cInitArray:32 cFillArray:22 cReverseArray:27 cDestroyArray:56 Total: 13826
cInitArray:31 cFillArray:22 cReverseArray:27 cDestroyArray:110 Total: 102158
cInitArray:73 cFillArray:29 cReverseArray:29 cDestroyArray:50 Total: 13927
cInitArray:43 cFillArray:23 cReverseArray:29 cDestroyArray:90 Total: 16840
cInitArray:40 cFillArray:23 cReverseArray:35 cDestroyArray:102 Total: 20860
cInitArray:48 cFillArray:23 cReverseArray:27 cDestroyArray:55 Total: 13885
cppInitArray:87 cppFillArray:3880 cppReverseArray:58 cppDestroyArray:187 Total: 52192
cppInitArray:52 cppFillArray:531 cppReverseArray:48 cppDestroyArray:71 Total: 16392
cppInitArray:31 cppFillArray:966 cppReverseArray:52 cppDestroyArray:93 Total: 972812
cppInitArray:39 cppFillArray:422 cppReverseArray:41 cppDestroyArray:68 Total: 16228
cppInitArray:133 cppFillArray:890 cppReverseArray:52 cppDestroyArray:85 Total: 1113920
cppInitArray:31 cppFillArray:412 cppReverseArray:41 cppDestroyArray:68 Total: 16260
cppInitArray:78 cppFillArray:520 cppReverseArray:43 cppDestroyArray:74 Total: 17256
cppInitArray:29 cppFillArray:408 cppReverseArray:41 cppDestroyArray:72 Total: 16457
cppInitArray:32 cppFillArray:405 cppReverseArray:41 cppDestroyArray:71 Total: 16465
cppInitArray:29 cppFillArray:627 cppReverseArray:48 cppDestroyArray:130 Total: 30847
objcInitArray:10039 objcFillArray:36587 objcReverseArray:8805 objcDestroyArray:663 Total: 77180
objcInitArray:184 objcFillArray:11808 objcReverseArray:4644 objcDestroyArray:543 Total: 35455
objcInitArray:337 objcFillArray:11199 objcReverseArray:5017 objcDestroyArray:777 Total: 1215882
objcInitArray:320 objcFillArray:10961 objcReverseArray:4694 objcDestroyArray:528 Total: 34369
objcInitArray:145 objcFillArray:17909 objcReverseArray:4717 objcDestroyArray:566 Total: 44012
objcInitArray:185 objcFillArray:10529 objcReverseArray:4615 objcDestroyArray:528 Total: 33762
objcInitArray:138 objcFillArray:10456 objcReverseArray:4610 objcDestroyArray:527 Total: 33629
objcInitArray:134 objcFillArray:11594 objcReverseArray:4735 objcDestroyArray:779 Total: 142890
objcInitArray:319 objcFillArray:10928 objcReverseArray:4712 objcDestroyArray:547 Total: 43185
objcInitArray:154 objcFillArray:12073 objcReverseArray:4646 objcDestroyArray:538 Total: 35622
Ignoring the anomalous statistics (mach_absolute_time can take a while to initialise), we have about 14000 µs total for C, 16500 µs in C++, and 30000-40000 µs in Objective-C. I don't think that's too bad given that Cocoa was having to use NSNumbers rather than ints - I'm happy with it being within the same order of magnitude.
That said, if efficiency was absolutely critical for certain functions, I'd definitely write those in pure C:)
(Last edited by Gul Banana; Dec 27, 2002 at 06:39 AM.
)
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
That's actually surprisingly fast for Obj-C.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Aug 2002
Location: Northeastern NV, USA
Status:
Offline
|
|
Isn't int[] still native for Objective-C? Correct me if I'm wrong, but I consider NSNumber to be a framework object and not a native data type.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by yeslekmc:
Isn't int[] still native for Objective-C? Correct me if I'm wrong, but I consider NSNumber to be a framework object and not a native data type.
Yes, and yes. What's your point?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
Testing int[] twice would have served no purpose - calling this a comparison of languages was wrong, really. What it really tests is the difference between int[], vector, and NSArray.
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|