-3

I have used Calendar.current.date(bySettingHour code for setting particular date. Problem is it takes so long to compile ~4 seconds

print("Time seconds ",Date().timeIntervalSince1970)
for i in 0..<9999 {
    let nowDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
}
print("Time seconds ",Date().timeIntervalSince1970)// For loop took 4 seconds

Is there any way to reduce compile time ?

Amit
  • 556
  • 1
  • 7
  • 24

1 Answers1

2

You can't test performance in a Playground, and most of all not compile performance. Playgrounds do a lot of extra work to display "(9999 times)" in the right-hand column. Playgrounds also don't really have a separate "compile" step that you can separate from execution. And they don't optimize the code. There's no part of performance that you can evaluate in a Playground.

When I compile this with swiftc it takes less than half a second. It runs in less than a second.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I had put this in xcode project & result is approximately same. Can you please check. – Amit Apr 11 '19 at 13:47
  • Tested. Takes less than a second to compile. Have you put exactly this code (plus `import Foundation`) into a project with nothing else in it? I generally use a command-line project to eliminate all other variables. How precisely are you creating your project, and are you using the current stable Xcode (10.2)? – Rob Napier Apr 11 '19 at 13:51
  • @RobNaiper Yes latest xcode. Please try with `99999` or more it becoming slow. Please help – Amit Apr 12 '19 at 12:42
  • 1
    @Amit To compile? If you're seeing a *compilation* difference due to changing a constant, something is very wrong, and you should post a complete example demonstrating it. If you mean it's slow to *run* code that creates 200k Date objects, 100k of them requiring non-trivial calendar calculations, and then deallocate the memory for each, that doesn't seem surprising. (Again, I recommend testing this in a Mac commandline app. How long this takes in a Playground isn't meaningful.) – Rob Napier Apr 12 '19 at 12:50
  • @RobNaiper thanks :) I am on xcode project. Actually i have calendar app which has years of data. when i jump towards debugger navigator it show 90% cpu usage while running on simulator. i have used this code in `DispatchQueue.global(qos: .background).async {` it does calculation in background. `UI` not freezing but 90% CPU usage is terrible process. Can we optimise ? Any suggestion ? – Amit Apr 12 '19 at 12:55
  • 1
    I do this kind of optimization work all the time, and be happy to discuss it further, but it would require working on the actual code, not something like the code. Performance optimizations are very use-specific. As a rule, I do that on a consulting basis rather than in StackOverflow, since most people cannot post their full project, and the answer is typically not helpful to others. If you can reduce the full problem to a small, self-contained example (such that fixing it would certainly solve your full problem), I'm happy to look at it here. Otherwise, I'm happy to discuss consulting. – Rob Napier Apr 12 '19 at 13:49
  • (As an example of what I'm describing by "would certainly solve your full problem," fixing the above performance issue is trivial: write a single Date to all the entries, since all are based on "now." But I'm certain that would not actually fix your "real" problem. So optimizing the code you've posted would not be helpful to you. Going back and forth with "this is faster" followed by "well, that doesn't work in my real project" is not practical on StackOverflow. Best of luck, and happy to discuss more if you like.) – Rob Napier Apr 12 '19 at 13:52