Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Note</strong>: Your example program is very much a micro-benchmark and very effectively maximally amplifies that cost of a date formatter. You are comparing <em>doing absolutely nothing</em> with <em>doing something</em>. Thus, whatever that <em>something</em> is, it will appear to be <strong>something times</strong> slower than <em>nothing</em>.</p> <p>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.</p> <blockquote> <p>What is the reason for such costs?</p> </blockquote> <pre><code>NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyyMMdd HH:mm:ss.SSS"]; </code></pre> <p>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 <code>NSDateFormatter</code> does. Cocoa has extremely thorough support for localization, but that support comes at a cost of complexity.</p> <p>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, <em>please</em> update your question!).</p> <blockquote> <p>Can there be a blocking where the threads have to wait for each other?</p> </blockquote> <p>I'm surprised it doesn't simply crash when you use a single formatter from multiple threads. <code>NSDateFormatter</code> doesn't specifically mention that it is thread safe. Thus, you must assume that it is not thread safe.</p> <blockquote> <p>How can I improve the usage?</p> </blockquote> <p>Don't create so many date formatters!</p> <p>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.</p> <p>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).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload