Earlier quoted context omitted.
good stuff. tbh though, only ~3x slowdown in scripting imo is pretty damn good 8)
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.
A Very Simple Genetic Algorithm Written in Swift 3
21–30 of 42 posts
Re: A Very Simple Genetic Algorithm Written in Swift 3
#22Re: A Very Simple Genetic Algorithm Written in Swift 3
#23> Note -- this is much slower than the python version Surprising. Swift, a compiled, static language, is slower than python in this use case?
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.
Re: A Very Simple Genetic Algorithm Written in Swift 3
#24> let POP_SIZE = 50 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... )
Re: A Very Simple Genetic Algorithm Written in Swift 3
#25I 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) https://github.com/JaviSoto/RubikSwift
I'm gonna give it a shot
Re: A Very Simple Genetic Algorithm Written in Swift 3
#26Earlier quoted context omitted.
This is just speculation, but slowness might be because this is being run as a script via /usr/bin/swift, rather than being precompiled into a binary. 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
ah great point. I'll time it both ways and see where I'm at.
https://dl.dropboxusercontent.com/u/13740348/Screen%20Shot%2...
Re: A Very Simple Genetic Algorithm Written in Swift 3
#27> Note -- this is much slower than the python version Surprising. Swift, a compiled, static language, is slower than python in this use case?
Re: A Very Simple Genetic Algorithm Written in Swift 3
#28Earlier quoted context omitted.
ah great point. I'll time it both ways and see where I'm at.
I compiled it down and did a quick profile. Looks like a lot of the time is spent on line 96, converting a Character to its ascii value. https://dl.dropboxusercontent.com/u/13740348/Screen%20Shot%2...
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.Re: A Very Simple Genetic Algorithm Written in Swift 3
#29> Note -- this is much slower than the python version Surprising. Swift, a compiled, static language, is slower than python in this use case?
My guess it is all of the string converting back and forth to character arrays.
https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b...
It now runs faster (when compiled, optimized) than the python code.
xcrun -sdk macosx swiftc -O gen.swiftRe: A Very Simple Genetic Algorithm Written in Swift 3
#30Earlier quoted context omitted.
ah great point. I'll time it both ways and see where I'm at.
I compiled it down and did a quick profile. Looks like a lot of the time is spent on line 96, converting a Character to its ascii value. https://dl.dropboxusercontent.com/u/13740348/Screen%20Shot%2...
https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b...