6 ms·
> Note -- this is much slower than the python version Surprising. Swift, a compiled, static language, is slower than python in this use case?
by hellofunk 10y ago
> Note -- this is much slower than the python version
Surprising. Swift, a compiled, static language, is slower than python in this use case?
- CorvusCrypto 10y agostuff like this can't help var n = Double(arc4random_uniform(UInt32(weightTotal * 1000000.0))) / 1000000.0
- brothrock 10y agoYep 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 :(.
- sdegutis 10y agoOut of curiosity, what's wrong with that line? Why is it slow?
- CorvusCrypto 10y agostarting 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.
- nicky0 10y agoThis 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
- brothrock 10y agoah great point. I'll time it both ways and see where I'm at.
- pkaler 10y agoI 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%202016-11-11%20at%2010.14.33%20AM.png https://dl.dropboxusercontent.com/u/13740348/Screen%20Shot%2...
- Someone 10y agoThat'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 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.
- brothrock 10y agoThis is awesome guys. Thanks.
- rookie 10y agoReplaced String with Int array https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b467 https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b...
- CorvusCrypto 10y agogood stuff. tbh though, only ~3x slowdown in scripting imo is pretty damn good 8)
- mpweiher 10y agoYeah. 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.
- rookie 10y agoMy guess it is all of the string converting back and forth to character arrays.
- rookie 10y agoYep, replaced string with Int array and now it is much faster. https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b467 https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b... It now runs faster (when compiled, optimized) than the python code. xcrun -sdk macosx swiftc -O gen.swift
- nicky0 10y agoOoh yeah, 0m0.363s for 1000 iterations on mine. So the slowness was in the string manipulation and character conversion operations.
- SOLAR_FIELDS 10y agoI've learned from years of working on making code more performant that the biggest code smells to look at first are string operations.