Live data from Hacker News

A Very Simple Genetic Algorithm Written in Swift 3

gist.github.com

11–20 of 42 posts

Re: A Very Simple Genetic Algorithm Written in Swift 3

#11
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

good stuff. tbh though, only ~3x slowdown in scripting imo is pretty damn good 8)

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

Re: A Very Simple Genetic Algorithm Written in Swift 3

#12
post #11

Earlier quoted context omitted.

good stuff. tbh though, only ~3x slowdown in scripting imo is pretty damn good 8)

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

btw was this after compiled to pyc?

Re: A Very Simple Genetic Algorithm Written in Swift 3

#13
post #10

Are we all just sharing everything we write on here now? Is this notable for some reason?

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.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#15

> Note -- this is much slower than the python version Surprising. Swift, a compiled, static language, is slower than python in this use case?

stuff like this can't help var n = Double(arc4random_uniform(UInt32(weightTotal * 1000000.0))) / 1000000.0

Out of curiosity, what's wrong with that line? Why is it slow?

Re: A Very Simple Genetic Algorithm Written in Swift 3

#16
post #10

Are we all just sharing everything we write on here now? Is this notable for some reason?

Shouldn't people should feel free to share whatever they want that's HN material?

That's why we vote articles up. Sometimes the comments alone in a basic post add a lot of value.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#17

Earlier quoted context omitted.

stuff like this can't help var n = Double(arc4random_uniform(UInt32(weightTotal * 1000000.0))) / 1000000.0

Out of curiosity, what's wrong with that line? Why is it slow?

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.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#18
post #10

Are we all just sharing everything we write on here now? Is this notable for some reason?

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

Re: A Very Simple Genetic Algorithm Written in Swift 3

#19
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

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.

Re: A Very Simple Genetic Algorithm Written in Swift 3

#20
post #16
post #10

Are we all just sharing everything we write on here now? Is this notable for some reason?

Shouldn't people should feel free to share whatever they want that's HN material? That's why we vote articles up. Sometimes the comments alone in a basic post add a lot of value.

Agreed. And voting the whole thing up.
Post reply on HN