Yep totally, going to take another pass to try and make it faster. This was just a make it work experiment. Had a few fun troubles with random, but at the moment it just crawls :(.
starting from inside the arc4random call you have double float multiplication, then cast to uint32, then the arc4rand call, then the double cast for the uint32 result and then a double float division. this adds up in instructions.
That's because Character is very, very different from ASCII in Swift (a character in Swift holds an extended grapheme cluster. See https://developer.apple.com/reference/swift/character). I also doubt the way they compute its ASCII value is optimal:
extension Character {
var asciiValue: UInt32? {
return String(self)
.unicodeScalars
.filter{$0.isASCII}
.first?
.value
}
}
Certainly if it is optimal, but probably also if it isn't, I would change the program to not do that conversion in inner loops.
Isn't the "scripting" part just automatically compiling behind the scenes before running? It's not an interpreter or a VM. So the code runs just as fast as the compiled version, it's just that you pay a startup cost.
That's true between a debug build and just running it as a script I believe. But a release version will be optimized, I imagine if they compared a debug build to the scripted version they'd perform similarly.
Yeah. Swift is unique in that it features both incredibly slow compile times (in the good case, in bad cases it can abort due to timeouts on one line, 40 character expressions) AND slow runtime speed, especially when not optimising.
Code without the optimiser can easily be several hundred to more than a thousand times slower than optimised code. And of course the optimiser makes compile times even slower.
I wanted an example of a simple genetic algorithm in swift, couldn't find it so I wrote it from something I found in python. 0% special, but thought i'd share. Take it or leave it, dude.
Just to add to this, if you don't like it, don't click on it/comment on it. The front page algorithm put this item here because people upvoted it, not because it queried a random selection of the user base and asked if it was "worthy".
I thought this was going to be a link to [1], from a talk given at Swift Summit a couple days ago. I haven't really looked at the code for either one to compare them, but for people who are interested, here's a second simple genetic algorithm in Swift (found in RubikSwift/GeneticsSolver.swift)
swift is a fantastic language, but it's absolutely not as mature as other language you're probably used to. In particular, you may experience a lot of compiler crash, typing bugs, and very bad compile time.
But i'm 100% certain it's going to be one of the most used language server side in the coming years.
Did not expect to see "cfdrake" when I initially clicked the link - nice port! :)
Agreed with e28eta - I actually saw RubikSwift demoed at Swift Summit a few days ago as well and enjoyed it. Definitely check it out if you're interested in the topic.
41 comments
[ 5.6 ms ] story [ 104 ms ] threadSurprising. Swift, a compiled, static language, is slower than python in this use case?
var n = Double(arc4random_uniform(UInt32(weightTotal * 1000000.0))) / 1000000.0
Certainly I have found my own Swift scripts seem to be a lot slower than compiled programs.
EDIT: from my own test with 1000 iterations, script version 17s, compiled version (release build) 5.4s
https://dl.dropboxusercontent.com/u/13740348/Screen%20Shot%2...
https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b...
That allows for more aggressive code specialization and inlining and can remove more reference count updates (https://swift.org/blog/whole-module-optimizations/)
Code without the optimiser can easily be several hundred to more than a thousand times slower than optimised code. And of course the optimiser makes compile times even slower.
It's really quite a spectacle.
https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b...
It now runs faster (when compiled, optimized) than the python code.
c'mon everyone knows genetic population is 30
(I kid I kid but that number has an interesting story https://statswithcats.wordpress.com/2010/07/11/30-samples-st... )
That's why we vote articles up. Sometimes the comments alone in a basic post add a lot of value.
https://github.com/JaviSoto/RubikSwift
I'm gonna give it a shot
http://www.h4labs.com/dev/ios/swift_cookbook.html
But i'm 100% certain it's going to be one of the most used language server side in the coming years.
Agreed with e28eta - I actually saw RubikSwift demoed at Swift Summit a few days ago as well and enjoyed it. Definitely check it out if you're interested in the topic.