Live data from Hacker News

A Very Simple Genetic Algorithm Written in Swift 3

gist.github.com

21–30 of 42 posts

Re: A Very Simple Genetic Algorithm Written in Swift 3

#21

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.

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.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#22
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)

https://github.com/JaviSoto/RubikSwift

Re: 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?

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.

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... )

It's not the population size, but the number of times you run the evolutionary algorithm with different seeds.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#25
post #22

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) https://github.com/JaviSoto/RubikSwift

Swift looks a lot like Typescript. This is the first time I've read swift code and I'm taken by surprise by its elegance.

I'm gonna give it a shot

Re: A Very Simple Genetic Algorithm Written in Swift 3

#26
post #4

Earlier 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.

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...

Re: A Very Simple Genetic Algorithm Written in Swift 3

#28
post #26

Earlier 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...

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.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#29
post #27

> 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.

Yep, replaced string with Int array and now it is much faster.

https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b...

It now runs faster (when compiled, optimized) than the python code.

    xcrun -sdk macosx swiftc -O gen.swift

Re: A Very Simple Genetic Algorithm Written in Swift 3

#30
post #26

Earlier 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...

Replaced String with Int array

https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b...

Post reply on HN