Live data from Hacker News

A Very Simple Genetic Algorithm Written in Swift 3

gist.github.com

31–40 of 42 posts

Re: A Very Simple Genetic Algorithm Written in Swift 3

#31
post #28
post #26

Earlier quoted context omitted.

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 i…

This is awesome guys. Thanks.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#34
post #11

Earlier quoted context omitted.

however for comparison, presented without comment: python version 0.708s

btw was this after compiled to pyc?

Not sure, I'm not seeing a .pyc file in the directory. Same every time I run it though.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#35
post #29
post #27

Earlier quoted context omitted.

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

Ooh yeah, 0m0.363s for 1000 iterations on mine. So the slowness was in the string manipulation and character conversion operations.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#36

Earlier quoted context omitted.

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.

Can confirm, Debug build runs in similar time to script.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#37
post #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

Here's a little Swift Cookbook to get more of a flavor:

http://www.h4labs.com/dev/ios/swift_cookbook.html

Re: A Very Simple Genetic Algorithm Written in Swift 3

#38
post #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

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.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#39
post #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.

[deleted]

Re: A Very Simple Genetic Algorithm Written in Swift 3

#40
post #36

Earlier quoted context omitted.

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.

Can confirm, Debug build runs in similar time to script.

Difference is that Release builds do whole-module optimization in Swift 3.

That allows for more aggressive code specialization and inlining and can remove more reference count updates (https://swift.org/blog/whole-module-optimizations/)

Post reply on HN