Re: Java vs C/Objective-C performance question
Re: Java vs C/Objective-C performance question
- Subject: Re: Java vs C/Objective-C performance question
- From: Marcel Weiher <email@hidden>
- Date: Mon, 17 Mar 2003 12:45:14 +0100
On Monday, March 17, 2003, at 08:34 Uhr, Sheehan Olver wrote:
Objective-C:
2003-03-16 16:10:43.426 CocoaTest[4012] Before 1: 2003-03-16 16:10:43
-0600
2003-03-16 16:11:08.745 CocoaTest[4012] After 1: 2003-03-16 16:11:08
-0600
2003-03-16 16:11:53.406 CocoaTest[4034] Before 2: 2003-03-16 16:11:53
-0600
2003-03-16 16:12:17.392 CocoaTest[4034] After 2: 2003-03-16 16:12:17
-0600
I took your code and compiled it once with no optimization ( -O0 ), and
once with optimization ( -O2 ). All tests were run on a dual G4/1GHz
PowerMac. The first loop goes from 12 seconds to 2 seconds, a factor 6
improvement, and probably needs a greater iteration count for usable
results. The second loop doesn't show much improvement, because you
are accessing library code that doesn't chage. However, as others have
pointed out, just allocating an object and immediately releasing it
afterward doesn't reflect real usage at all.
--------------- snip -------------
marcel[Misc]cc -Wall -O0 -o slow-c-test slow-c-test.m -framework
Foundation
slow-c-test.m: In function `main':
slow-c-test.m:6: warning: unused variable `pool'
marcel[Misc]./slow-c-test
2003-03-17 12:25:24.729 slow-c-test[24688] Before 1: 2003-03-17
12:25:24 +0100
2003-03-17 12:25:36.877 slow-c-test[24688] After 1: 2003-03-17 12:25:36
+0100
2003-03-17 12:25:36.877 slow-c-test[24688] Before 2: 2003-03-17
12:25:36 +0100
2003-03-17 12:25:49.269 slow-c-test[24688] After 2: 2003-03-17 12:25:49
+0100
marcel[Misc]cc -Wall -O2 -o slow-c-test slow-c-test.m -framework
Foundation
slow-c-test.m: In function `main':
slow-c-test.m:6: warning: unused variable `pool'
marcel[Misc]./slow-c-test
2003-03-17 12:26:04.385 slow-c-test[24714] Before 1: 2003-03-17
12:26:04 +0100
2003-03-17 12:26:06.408 slow-c-test[24714] After 1: 2003-03-17 12:26:06
+0100
2003-03-17 12:26:06.409 slow-c-test[24714] Before 2: 2003-03-17
12:26:06 +0100
2003-03-17 12:26:18.891 slow-c-test[24714] After 2: 2003-03-17 12:26:18
+0100
------------- snip ------------
However, the second loop does show that basic object-allocation is a
very expensive operation in Objective-C, so it might be an issue to
have lots of temporary objects created and destroyed in a
performance-sensitive loop.
Luckily, Objective-C is flexible enough that we can help ourselves, and
MPWObjectCache ( in MPWFoundation ) helps optimize temporary
(short-lived) object-creation very much like modern GCs do. Using such
a cache (and -O2 optimization), the results are as follows:
---------- snip ------
marcel@[Misc]cc -Wall -O2 -o slow-c-test slow-c-test.m -framework
MPWFoundation -framework Foundation
slow-c-test.m: In function `main':
slow-c-test.m:6: warning: unused variable `pool'
marcel@[Misc]./slow-c-test
2003-03-17 12:38:45.220 slow-c-test[25085] Before 1: 2003-03-17
12:38:45 +0100
2003-03-17 12:38:47.246 slow-c-test[25085] After 1: 2003-03-17 12:38:47
+0100
2003-03-17 12:38:47.247 slow-c-test[25085] Before 2: 2003-03-17
12:38:47 +0100
2003-03-17 12:38:48.146 slow-c-test[25085] After 2: 2003-03-17 12:38:48
+0100
--------- snip -------
This makes the time for the second loop drop to 1 second, again
suggesting an increase in the iteration count is called-for.
To use the MPWObjectCache, the code was modified as follows:
[..declaration of the cache..]
MPWObjectCache* cache;
[..initialization and use of the cache..]
cache = [[[MPWObjectCache alloc] initWithCapacity:10
class:[NSObject class]] autorelease];
[cache setUnsafeFastAlloc:YES];
for(j = 0; j < 1000; j++) {
for(i = 0; i < 10000; i++) {
GETOBJECT( cache );
}
}
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.