-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slow initialization of String objects from Collection/Sequence #63
Comments
Yes, that var s = ""
var t = ""
var a = [Int]()
for x in 1...1000000 {
s += String(UnicodeScalar(UInt8(((x - 1) % 26) + 65)))
if s.count == 26 {
t += s
s = ""
}
}
var r = Array(t)
let count = t.count
for i in 0..<count/2 {
r.swapAt(i, count - ((i + 1) as Int))
}
let str=String(r) // <-------- +5:00 It should be an O(n) operation with some additional conversions performed on all 32-bit platforms, but clearly there is something more going on. Corner case as it may be, I'd open a ticket on https://bugs.swift.org/. |
I was able to get it down to 23 seconds. // Swift 5.1.2 - 1mil3-3.swift
var s = ""
var t = ""
var a = [Int](repeating: 0, count: 1000001)
for x in 1...1000000 {
let c = (x - 1) % 26
s.append(String(UnicodeScalar(UInt8(c + 65))))
a[x] = x
if c == 25 {
t.append(String(s.reversed()))
s = ""
}
}
print("t LEN: ", t.count)
print("Front: \(t.prefix(26))")
print("Back: \(t.suffix(26))")
print("UBVal: ", a[100000]) pi@RPi4B:$ swiftc -O 1mil3-3.swift |
Hmm, the resulting string should be the same, but you are now creating 25 chars strings instead of using the 1million characters string you had before. The main issue here is that the performance of the initializer does not seem to decrease linearly as the string grows, it’s worse than that. |
I installed Ubuntu 19.10 64 bit server on the RPi 4B 4GB and Swift now runs as I would have expected and more inline with what I'm seeing on my Lenovo laptop. ubuntu@rpi4b:$ swiftc -O 1mil4.swift |
Hi, I know this issue already has a few months, but I wanted to share my experience too: Raspberry Pi 4, Swift 5.1, Raspbian (armv7l): I can also say string manipulation, specially Could this be related to the libicu dependency in any way? |
It seems Swift for the RPi has performance issues. The following program is a good example.
RPI 4B 4GB
(Without String reverse)
Ubuntu Lenovo Laptop
Swift 4.1.5 Windows Lenovo Laptop
The Swift Online Playground also returns a result almost instantly.
Something is horribly wrong IMHO.
The text was updated successfully, but these errors were encountered: