NSArchiver 与 NSKeyedArchiver 性能对比

发布于 2024-12-25 20:32:52 字数 1401 浏览 2 评论 0原文

为什么 NSKeyedArchiver 性能这么差?与使用 NSArchiver 相比,大小增加了一倍。

我使用以下行对对象的 NSMutableArray 进行编码

BOOL result = [NSArchiver archiveRootObject:self.appDataObject.materias toFile:archivePath];

NSMutableArray 包含具有相应的encodeWithCoder 和 initWithCoder 的自定义对象,

-(void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject: _fileName];
    [aCoder encodeObject: _categoria];
    [aCoder encodeObject: _materia];
    [aCoder encodeObject: _nombre];

    [aCoder encodeObject: _position];
    [aCoder encodeValueOfObjCType:@encode(BOOL) at:&_favorite];

}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super init]) {
        [self setFileName:[aDecoder decodeObject]];
        [self setCategoria:[aDecoder decodeObject]];
        [self setMateria:[aDecoder decodeObject]];
        [self setNombre:[aDecoder decodeObject]];

        [self setPosition:[aDecoder decodeObject]];
        [aDecoder decodeValueOfObjCType:@encode(BOOL) at:&_favorite];
    }

    return self;
}

它工作正常,因为它可以正确保存文件,然后我可以将其取消存档。它们大约有 3000 个对象,输出文件约为 900kB

当我将归档行更改为:

BOOL result = [NSKeyedArchiver archiveRootObject:self.appDataObject.materias toFile:archivePath];

一切都神奇地工作但是文件大小增加了一倍多到 >2MB

我为什么要问这个?因为我正在开发一个iOS应用程序,因此失去了NSArchiver的支持。

Why is NSKeyedArchiver performance so poor? The size doubles vs using NSArchiver.

I am encoding an NSMutableArray of objects with the following line

BOOL result = [NSArchiver archiveRootObject:self.appDataObject.materias toFile:archivePath];

the NSMutableArray contain custom objects that have their corresponding encodeWithCoder and initWithCoder

-(void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject: _fileName];
    [aCoder encodeObject: _categoria];
    [aCoder encodeObject: _materia];
    [aCoder encodeObject: _nombre];

    [aCoder encodeObject: _position];
    [aCoder encodeValueOfObjCType:@encode(BOOL) at:&_favorite];

}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super init]) {
        [self setFileName:[aDecoder decodeObject]];
        [self setCategoria:[aDecoder decodeObject]];
        [self setMateria:[aDecoder decodeObject]];
        [self setNombre:[aDecoder decodeObject]];

        [self setPosition:[aDecoder decodeObject]];
        [aDecoder decodeValueOfObjCType:@encode(BOOL) at:&_favorite];
    }

    return self;
}

it works fine as it saves the file properly and then I am able to unarchive it. They are around 3000 objects and the output file is about 900kB

The problem occurs when I change my archiving line to:

BOOL result = [NSKeyedArchiver archiveRootObject:self.appDataObject.materias toFile:archivePath];

Everything magically works BUT the file size more than doubles to 2MB!

Why am I asking this? because I am developing a iOS application and therefore lose support of NSArchiver.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2025-01-01 20:32:52

至于大小的减小,我认为这与 NSKeyedArchiver 存储引用而不是实际值有关。

我比较了 NSKeyedArchiver 和 NSArchiver,以及一个名为 NSKeylessArchiver 的新类,该类基于 cocotron(由于私有 API 状态,NSArchiver 可能在 iOS 上受到限制)。 NSKeylessArchiver 使用引用来减少具有重复字符串或对象的数据的大小,如果不需要密钥,则它是更快的解码替代方案。

没有子对象和 20000 个整数的简单根对象的性能:

|                 |encoding (min/max/avg secs)|decoding (min/max/avg secs)|
|-----------------|:-------------------------:|:-------------------------:|
|NSKeyedArchiver  |  0.2048/0.2453/0.2165     |  6.8919/6.9238/6.9037|
|NSKeylessArchiver|  0.0407/0.0506/0.0451     |  0.0253/0.0330/0.0287|
|NSArchiver       |  0.0094/0.0114/0.0102     |  0.0019/0.0025/0.0020|

有关更多详细信息,请参阅 Github 存储库博客发布

As for the size reduction, I believe it has something to do with NSKeyedArchiver storing references as opposed to actual values.

I compared the NSKeyedArchiver and NSArchiver, along with a new class called NSKeylessArchiver that is based off of cocotron (NSArchiver may be off-limits on iOS due to private API status). NSKeylessArchiver uses references to reduce size for data that has repeated strings or objects, and is much faster decoding alternative if you do not require keys.

Performance for simple root object without children and 20000 ints:

|                 |encoding (min/max/avg secs)|decoding (min/max/avg secs)|
|-----------------|:-------------------------:|:-------------------------:|
|NSKeyedArchiver  |  0.2048/0.2453/0.2165     |  6.8919/6.9238/6.9037|
|NSKeylessArchiver|  0.0407/0.0506/0.0451     |  0.0253/0.0330/0.0287|
|NSArchiver       |  0.0094/0.0114/0.0102     |  0.0019/0.0025/0.0020|

For more details, see the Github repo and blog post.

请恋爱 2025-01-01 20:32:52

您将获得向前和向后兼容性。它是键控的,这意味着存档必须存储更多信息才能进行键控查找。请参阅此处的参考:存档和序列化

You are gaining both forward and backward compatibility. It's keyed, which means the archive has to store more information to do keyed lookups. See the ref here: Archives and Serialization

听你说爱我 2025-01-01 20:32:52

我知道这个线程很旧,但你我想考虑使用 NSPropertyListSerialization...

看看这个线程中提到的性能差异:
http://www.cocoabuilder.com/archive/cocoa /2221-nspropertylistserialization-vs-nskeyedarchiver-performance.html

I know this thread is old, but you my want to consider using NSPropertyListSerialization...

Take a look at the performance difference as mentioned in this thread:
http://www.cocoabuilder.com/archive/cocoa/2221-nspropertylistserialization-vs-nskeyedarchiver-performance.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文