Alternative to UnicodeScalar


(Proyb P) #1

I have found this took about 0.001s to print 256 characters
Compare to Python took 0.000016s to print 256 characters, see F8 code and
have modify to run only one call instead of 1000 iterations.
https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4d3080

As this is frequently use for server-side swift that will definitely
affected by expensive call, are there any alternative solution for
converting Int to character close to Python timing?

import Foundation
var display: String = ""

    func printTimeElapsedWhenRunningCode(title: String, operation: ()->()) {
        let startTime = CFAbsoluteTimeGetCurrent()
        operation()
        let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
        print("Time elapsed for \(title): \(timeElapsed) s")
    }

    printTimeElapsedWhenRunningCode(title: "s1()") {
let startingValue = Int(("A" as UnicodeScalar).value) // 65
for i in 0 ..< 256 {
    print(Character(UnicodeScalar(i + startingValue)!))
}
}


(Shawn Erickson) #2

What optimization level did you try this at? You are also including the
print func in your performance test which is a non-trivial amount of code
being executed. Also when in doubt profile the code to see the time spent
to better nail down the hotspot locations.

If performance issues exist then leveraging things like a lookup table of
pre-built structure can help, etc.

-Shawn

···

On Sat, Apr 8, 2017 at 9:19 AM Proyb P via swift-users < swift-users@swift.org> wrote:

I have found this took about 0.001s to print 256 characters
Compare to Python took 0.000016s to print 256 characters, see F8 code and
have modify to run only one call instead of 1000 iterations.
https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4d3080

As this is frequently use for server-side swift that will definitely
affected by expensive call, are there any alternative solution for
converting Int to character close to Python timing?

import Foundation
var display: String = ""

    func printTimeElapsedWhenRunningCode(title: String, operation: ()->())
{
        let startTime = CFAbsoluteTimeGetCurrent()
        operation()
        let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
        print("Time elapsed for \(title): \(timeElapsed) s")
    }

    printTimeElapsedWhenRunningCode(title: "s1()") {
let startingValue = Int(("A" as UnicodeScalar).value) // 65
for i in 0 ..< 256 {
    print(Character(UnicodeScalar(i + startingValue)!))
}
}
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #3

Server-side is usually UTF-8 (not always), so often you don’t really need strings.
As an aside, time measurements are difficult, especially when IO is involved there may be thread switches or locks.

I have written some parsers (UTF-8 based) and not yet encountered performance problems. (though faster is always better)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift

···

On 08 Apr 2017, at 18:19, Proyb P via swift-users <swift-users@swift.org> wrote:

I have found this took about 0.001s to print 256 characters
Compare to Python took 0.000016s to print 256 characters, see F8 code and have modify to run only one call instead of 1000 iterations.
https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4d3080

As this is frequently use for server-side swift that will definitely affected by expensive call, are there any alternative solution for converting Int to character close to Python timing?

import Foundation
var display: String = ""

    func printTimeElapsedWhenRunningCode(title: String, operation: ()->()) {
        let startTime = CFAbsoluteTimeGetCurrent()
        operation()
        let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
        print("Time elapsed for \(title): \(timeElapsed) s")
    }

    printTimeElapsedWhenRunningCode(title: "s1()") {
let startingValue = Int(("A" as UnicodeScalar).value) // 65
for i in 0 ..< 256 {
    print(Character(UnicodeScalar(i + startingValue)!))
}
}
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Proyb P) #4

I ran with -Ounchecked as most benchmark do, are there more optimization
level I'm not aware?

···

On Sun, Apr 9, 2017 at 12:44 AM, Shawn Erickson <shawnce@gmail.com> wrote:

What optimization level did you try this at? You are also including the
print func in your performance test which is a non-trivial amount of code
being executed. Also when in doubt profile the code to see the time spent
to better nail down the hotspot locations.

If performance issues exist then leveraging things like a lookup table of
pre-built structure can help, etc.

-Shawn

On Sat, Apr 8, 2017 at 9:19 AM Proyb P via swift-users < > swift-users@swift.org> wrote:

I have found this took about 0.001s to print 256 characters
Compare to Python took 0.000016s to print 256 characters, see F8 code and
have modify to run only one call instead of 1000 iterations.
https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4d3080

As this is frequently use for server-side swift that will definitely
affected by expensive call, are there any alternative solution for
converting Int to character close to Python timing?

import Foundation
var display: String = ""

    func printTimeElapsedWhenRunningCode(title: String, operation:
()->()) {
        let startTime = CFAbsoluteTimeGetCurrent()
        operation()
        let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
        print("Time elapsed for \(title): \(timeElapsed) s")
    }

    printTimeElapsedWhenRunningCode(title: "s1()") {
let startingValue = Int(("A" as UnicodeScalar).value) // 65
for i in 0 ..< 256 {
    print(Character(UnicodeScalar(i + startingValue)!))
}
}
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Proyb P) #5

Agree, in some case, I seem to find Cchar is really shine at 10x faster
than Python from A to Z, 1000x faster than normal Swift? Not sure if there
was flaw in the time measurement.

···

On Sunday, 9 April 2017, Rien <Rien@balancingrock.nl> wrote:

Server-side is usually UTF-8 (not always), so often you don’t really need
strings.
As an aside, time measurements are difficult, especially when IO is
involved there may be thread switches or locks.

I have written some parsers (UTF-8 based) and not yet encountered
performance problems. (though faster is always better)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift

> On 08 Apr 2017, at 18:19, Proyb P via swift-users <swift-users@swift.org > <javascript:;>> wrote:
>
> I have found this took about 0.001s to print 256 characters
> Compare to Python took 0.000016s to print 256 characters, see F8 code
and have modify to run only one call instead of 1000 iterations.
> https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4d3080
>
> As this is frequently use for server-side swift that will definitely
affected by expensive call, are there any alternative solution for
converting Int to character close to Python timing?
>
> import Foundation
> var display: String = ""
>
> func printTimeElapsedWhenRunningCode(title: String, operation:
()->()) {
> let startTime = CFAbsoluteTimeGetCurrent()
> operation()
> let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
> print("Time elapsed for \(title): \(timeElapsed) s")
> }
>
> printTimeElapsedWhenRunningCode(title: "s1()") {
> let startingValue = Int(("A" as UnicodeScalar).value) // 65
> for i in 0 ..< 256 {
> print(Character(UnicodeScalar(i + startingValue)!))
> }
> }
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org <javascript:;>
> https://lists.swift.org/mailman/listinfo/swift-users


(Proyb P) #6

How do I append character A-Z by integer into CChar and benchmark timing
how fast it’s append compare to UInt8?

var tmp: [CChar] = [0,0]

for var i in 0...256 {

tmp[0] = 100

}

print(String(cString: &tmp))

···

On Sun, Apr 9, 2017 at 3:17 AM, Proyb P <proyb6@gmail.com> wrote:

Agree, in some case, I seem to find Cchar is really shine at 10x faster
than Python from A to Z, 1000x faster than normal Swift? Not sure if there
was flaw in the time measurement.

On Sunday, 9 April 2017, Rien <Rien@balancingrock.nl> wrote:

Server-side is usually UTF-8 (not always), so often you don’t really need
strings.
As an aside, time measurements are difficult, especially when IO is
involved there may be thread switches or locks.

I have written some parsers (UTF-8 based) and not yet encountered
performance problems. (though faster is always better)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift

> On 08 Apr 2017, at 18:19, Proyb P via swift-users < >> swift-users@swift.org> wrote:
>
> I have found this took about 0.001s to print 256 characters
> Compare to Python took 0.000016s to print 256 characters, see F8 code
and have modify to run only one call instead of 1000 iterations.
> https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4d3080
>
> As this is frequently use for server-side swift that will definitely
affected by expensive call, are there any alternative solution for
converting Int to character close to Python timing?
>
> import Foundation
> var display: String = ""
>
> func printTimeElapsedWhenRunningCode(title: String, operation:
()->()) {
> let startTime = CFAbsoluteTimeGetCurrent()
> operation()
> let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
> print("Time elapsed for \(title): \(timeElapsed) s")
> }
>
> printTimeElapsedWhenRunningCode(title: "s1()") {
> let startingValue = Int(("A" as UnicodeScalar).value) // 65
> for i in 0 ..< 256 {
> print(Character(UnicodeScalar(i + startingValue)!))
> }
> }
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #7

With the Array operations: append or insert

var tmp: [CChar] = [0, 0]
for i in 1 ... 100 {
    tmp.insert(0, at: 0)
}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift

···

On 09 Apr 2017, at 06:33, Proyb P <proyb6@gmail.com> wrote:

How do I append character A-Z by integer into CChar and benchmark timing how fast it’s append compare to UInt8?

var tmp: [CChar] = [0,0]
for var i in 0...256 {
   tmp[0] = 100
}
print(String(cString: &tmp))

On Sun, Apr 9, 2017 at 3:17 AM, Proyb P <proyb6@gmail.com> wrote:
Agree, in some case, I seem to find Cchar is really shine at 10x faster than Python from A to Z, 1000x faster than normal Swift? Not sure if there was flaw in the time measurement.

On Sunday, 9 April 2017, Rien <Rien@balancingrock.nl> wrote:
Server-side is usually UTF-8 (not always), so often you don’t really need strings.
As an aside, time measurements are difficult, especially when IO is involved there may be thread switches or locks.

I have written some parsers (UTF-8 based) and not yet encountered performance problems. (though faster is always better)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift

> On 08 Apr 2017, at 18:19, Proyb P via swift-users <swift-users@swift.org> wrote:
>
> I have found this took about 0.001s to print 256 characters
> Compare to Python took 0.000016s to print 256 characters, see F8 code and have modify to run only one call instead of 1000 iterations.
> https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4d3080
>
> As this is frequently use for server-side swift that will definitely affected by expensive call, are there any alternative solution for converting Int to character close to Python timing?
>
> import Foundation
> var display: String = ""
>
> func printTimeElapsedWhenRunningCode(title: String, operation: ()->()) {
> let startTime = CFAbsoluteTimeGetCurrent()
> operation()
> let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
> print("Time elapsed for \(title): \(timeElapsed) s")
> }
>
> printTimeElapsedWhenRunningCode(title: "s1()") {
> let startingValue = Int(("A" as UnicodeScalar).value) // 65
> for i in 0 ..< 256 {
> print(Character(UnicodeScalar(i + startingValue)!))
> }
> }
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users