Re: How to encode STL vector
Re: How to encode STL vector
- Subject: Re: How to encode STL vector
- From: "Clark S. Cox III" <email@hidden>
- Date: Fri, 8 Nov 2002 08:30:11 -0500
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Thursday, Nov 7, 2002, at 17:14 US/Eastern, Chris Hanson wrote:
After all this discussion, wouldn't it have been simpler to use an
NSArray?
-- Chris
-- ha ha, only serious... ;)
-- [coder encodeObject:myArray];
Well, here's a serious answer. The main two reasons to use vector over
NSArray are:
1) Speed. When doing lots of calculations, vector<unsigned> can be
orders of magnitude faster than using an NSArray filled with NSNumbers.
As an example:
2) Compatability with already written C++ code
The main reason to use NSArray over vector
1) Ease of use in Cocoa (everything's retained, single call to
encodeObject: as in your example, etc.)
Here is a simple comparason:
Using NSArray:
void AddArrays(NSArray *firstArray, NSArray *secondArray,
NSMutableArray *result)
{
NSEnumerator *firstEnumerator = [firstArray objectEnumerator];
NSEnumerator *secondEnumerator = [secondArray objectEnumerator];
NSNumber *firstNumber;
NSNumber *secondNumber;
[result removeAllObjects];
while(firstNumber = [firstEnumerator nextObject] && secondNumber =
[secondEnumerator nextObject])
{
unsigned firstValue = [firstNumber unsignedIntValue];
unsigned secondValue = [secondNumber unsignedIntValue];
unsigned newValue = firstValue + secondValue;
NSNumber *newNumber = [[NSNumber alloc] initWithUnsignedInt:
newValue];
[result addObject: newNumber];
[newNumber release];
}
}
Using vector:
void AddVectors(const std::vector<unsigned> &firstArray, const
std::vector<unsigned> &secondArray, std::vector<unsigned> & result)
{
std::vector<unsigned>::const_iterator firstIterator =
firstArray.begin();
std::vector<unsigned>::const_iterator secondIterator=
secondArray.begin();
result.clear();
while(firstIterator != firstArray.end() && secondIterator !=
secondArray.end())
{
result.push_back(*firstIterator++ + *secondIterator++);
}
}
Or, in a more compact version:
void AddVectors2(const std::vector<unsigned> &firstArray, const
std::vector<unsigned> &secondArray, std::vector<unsigned> & result)
{
result.clear();
result.resize(std::min(firstArray.size(),secondArray.size()));
std::transform(firstArray.begin(),firstArray.end(),secondArray.begin(),
result.begin(), std::plus<unsigned>);
}
The difference:
NSArray: 4 method calls + 8 method calls per loop (plus many more
behind the scenes (like addObject: calling retain)
vector: 0 function or method calls (All of the STL's functions are
inline)
Besides the overhead of the method calls the actual work being done
(the addition) is just as efficient in each case, however with each
method call actually executing a minimum of 30 instructions (if the
method is already cached) to many hundreds of instructions the first
time it is called. Even that minimum number can be a BIG overhead in a
tightly rolled loop..
- --
http://homepage.mac.com/clarkcox3/
email@hidden
Clark S. Cox, III
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (Darwin)
iEYEARECAAYFAj3LvGgACgkQd6STocYT1xX1BwCeOKMOiUwcJaIMQMJNN69UYg4K
LroAn1gH6HeOvYzxyw3i6rvDN604CFr8
=2w4J
-----END PGP SIGNATURE-----
_______________________________________________
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.