如何最小化分配和初始化NSDateFormatter的成本? [英] How to minimize the costs for allocating and initializing an NSDateFormatter?

查看:174
本文介绍了如何最小化分配和初始化NSDateFormatter的成本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到使用 NSDateFormatter 可能是相当昂贵的。我想,分配和初始化对象已经消耗了很多时间。

此外,似乎在多线程中使用 NSDateFormatter 会增加成本。是否可以有一个阻塞在哪里的线程必须等待对方?



我创建了一个小测试应用程序来说明问题。请检查一下。





这些费用的原因是什么,如何提高使用率?






17.12。 - 更新我的观察:我不明白为什么线程运行更长时处理并行相比,当运行在连续顺序。

:您的示例程序非常多,只有在使用NSDateFormatter时才会出现时差。

解决方案

微基准和非常有效地最大限度地放大日期格式化器的成本。你正在比较绝对不做任何事情做某事。因此,无论某事是什么,它似乎是某些时候没有更慢。


$ b b

这样的测试是非常有价值的,并且非常具有误导性。微观基准通常只在你有一个真实世界的情况下有用。如果你要使这个基准速度快10倍(事实上,你可能会用我下面的建议),但现实世界的情况下,只有1%的总体CPU时间在应用程序中使用,最终的结果不会是


这种费用的原因是什么?




  NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@yyyyMMdd HH:mm:ss.SSS];

最有可能的是,成本与必须解析/验证日期格式字符串相关联,做任何类型的区域设置特定goop NSDateFormatter 。 Cocoa对本地化提供了非常全面的支持,但是这种支持带来了复杂性的代价。



看到你写了一个相当不错的示例程序,你可以启动你的应用程序在仪器中,尝试各种CPU采样仪器,以了解什么是消耗CPU周期和仪器如何工作(如果你发现有趣的,请更新你的问题!)。




p>我很惊讶,它不会简单崩溃当你使用单个格式化器从多个线程。 NSDateFormatter 没有特别提到它是线程安全的。因此,您必须假设它不是线程安全的。


如何提高用法?


不要创建这么多日期格式化程序!



或者,如果你一直使用em,在你的应用程序运行开始时创建一个,并保持不变,直到格式改变。



对于线程,保持每个线程一个,如果你真的有必要(我敢打赌,这是过度的 - 你的应用程序的架构是这样的,每批操作将创建一个更明智的)。


I noticed that using an NSDateFormatter can be quite costly. I figured out that allocating and initializing the object already consumes a lot of time.
Further, it seems that using an NSDateFormatter in multiple threads increases the costs. Can there be a blocking where the threads have to wait for each other?

I created a small test application to illustrate the problem. Please check it out.

What is the reason for such costs and how can I improve the usage?


17.12. - To update my observation: I do not understand why the threads run longer when processed parallel compared to when the run in serial order. The time difference only occurs when NSDateFormatter is used.

解决方案

Note: Your example program is very much a micro-benchmark and very effectively maximally amplifies that cost of a date formatter. You are comparing doing absolutely nothing with doing something. Thus, whatever that something is, it will appear to be something times slower than nothing.

Such tests are extremely valuable and extremely misleading. Micro-benchmarks are generally only useful when you have a real world case of Teh Slow. If you were to make this benchmark 10x faster (which, in fact, you probably could with what I suggest below) but the real world case is only 1% of overall CPU time used in your app, the end result is not going to be a dramatic speed improvement -- it will be barely noticeable.

What is the reason for such costs?

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd HH:mm:ss.SSS"];

Most likely, the cost is associated with both having to parse/validate the date format string and having to do any kind of locale specific goop that NSDateFormatter does. Cocoa has extremely thorough support for localization, but that support comes at a cost of complexity.

Seeing as how you wrote a rather awesome example program, you could fire up your app in Instruments and try the various CPU sampling instruments to both understand what is consuming CPU cycles and how Instruments works (if you find anything interesting, please update your question!).

Can there be a blocking where the threads have to wait for each other?

I'm surprised it doesn't simply crash when you use a single formatter from multiple threads. NSDateFormatter doesn't specifically mention that it is thread safe. Thus, you must assume that it is not thread safe.

How can I improve the usage?

Don't create so many date formatters!

Either keep one around for a batch of operations and then get rid of it or, if you use 'em all the time, create one at the beginning of your app's run and keep around until the format changes.

For threading, keep one per thread around, if you really really have to (I'd bet that is excessive -- that the architecture of your app is such that creating one per batch of operations will be more sensible).

这篇关于如何最小化分配和初始化NSDateFormatter的成本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆