Last active
December 22, 2016 13:44
-
-
Save erica/ba8d60ec26c5459b9d3b4330da926c07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// Prints the elapsed time to execute a block under whatever optimization | |
/// conditions are currently in use by the compiler | |
public func timetest(_ note: String, block: () -> Void) { | |
print("Starting Test:", note) | |
let now = ProcessInfo().systemUptime | |
block() | |
let timeInterval = ProcessInfo().systemUptime - now | |
print("Ending Test:", note); print("Elapsed time: \(timeInterval)") | |
} | |
func rndLet() -> String { | |
func rnd(n: Int) -> Int { return Int(arc4random_uniform(UInt32(n))) } | |
let alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
let startIdx = alpha.index(alpha.startIndex, offsetBy: rnd(n: 26)) | |
return alpha | |
.substring(to: alpha.index(after: startIdx)) | |
.substring(from: startIdx) | |
} | |
let letters = (1 ... 200).map({ _ in rndLet() }) | |
5000 tests | |
// Biappi | |
timetest("Biappi") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = letters.reduce(Set<String>()) { | |
$0.union(CollectionOfOne($1)) | |
} | |
} | |
} | |
// Me | |
timetest("Erica") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = Set(letters) | |
} | |
} | |
-onone tests | |
Starting Test: Biappi | |
Ending Test: Biappi | |
Elapsed time: 2.55736974999309 | |
Starting Test: Erica | |
Ending Test: Erica | |
Elapsed time: 0.579013284004759 | |
Program ended with exit code: 0 | |
whole module tests | |
Starting Test: Biappi | |
Ending Test: Biappi | |
Elapsed time: 0.196299618983176 | |
Starting Test: Erica | |
Ending Test: Erica | |
Elapsed time: 0.115723677008646 | |
Program ended with exit code: 0 | |
Testing as streams (whole module) | |
// Biappi | |
timetest("Biappi") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = (1 ... 200).map({ _ in rndLet() }) | |
.lazy.reduce(Set<String>()) { | |
$0.union(CollectionOfOne($1)) | |
} | |
} | |
} | |
// Me | |
timetest("Erica - Insert") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
(1 ... 200).map({ _ in rndLet() }) | |
.lazy.forEach { x.insert($0) } | |
} | |
} | |
// Me | |
timetest("Erica - unlazy") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = Set((1 ... 200).map({ _ in rndLet() })) | |
} | |
} | |
Starting Test: Biappi | |
Ending Test: Biappi | |
Elapsed time: 1.60059031497804 | |
Starting Test: Erica - Insert | |
Ending Test: Erica - Insert | |
Elapsed time: 1.48718129799818 | |
Starting Test: Erica - unlazy | |
Ending Test: Erica - unlazy | |
Elapsed time: 1.54097430501133 | |
Program ended with exit code: 0 | |
Starting Test: Biappi | |
Ending Test: Biappi | |
Elapsed time: 1.52741046401206 | |
Starting Test: Erica - Insert | |
Ending Test: Erica - Insert | |
Elapsed time: 1.40471916401293 | |
// | |
// main.swift | |
// SetTests | |
// | |
// Created by Erica Sadun on 12/19/16. | |
// Copyright © 2016 Erica Sadun. All rights reserved. | |
// | |
import Foundation | |
/// Prints the elapsed time to execute a block under whatever optimization | |
/// conditions are currently in use by the compiler | |
public func timetest(_ note: String, block: () -> Void) { | |
print("Starting Test:", note) | |
let now = ProcessInfo().systemUptime | |
block() | |
let timeInterval = ProcessInfo().systemUptime - now | |
print("Ending Test:", note); print("Elapsed time: \(timeInterval)") | |
} | |
func rndLet() -> String { | |
func rnd(n: Int) -> Int { return Int(arc4random_uniform(UInt32(n))) } | |
let alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
let startIdx = alpha.index(alpha.startIndex, offsetBy: rnd(n: 26)) | |
return alpha | |
.substring(to: alpha.index(after: startIdx)) | |
.substring(from: startIdx) | |
} | |
let letters = (1 ... 200).map({ _ in rndLet() }) | |
// Biappi | |
timetest("Biappi - precalc letters") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = letters.reduce(Set<String>()) { | |
$0.union(CollectionOfOne($1)) | |
} | |
} | |
} | |
// Me | |
timetest("Erica - precalc letters, initializer") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
// x = Set(Set(Set(Set(Set(Set(Set(Set(Set(Set(letters)))))))))) // no change in time | |
x = Set(letters) | |
} | |
} | |
// Me | |
timetest("Erica - precalc letters, insertion") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
// x = Set(Set(Set(Set(Set(Set(Set(Set(Set(Set(letters)))))))))) // no change in time | |
letters.forEach({ x.insert($0) }) | |
} | |
} | |
// Biappi | |
timetest("Biappi") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = (1 ... 200).map({ _ in rndLet() }) | |
.lazy.reduce(Set<String>()) { | |
$0.union(CollectionOfOne($1)) | |
} | |
} | |
} | |
// Me | |
timetest("Erica - Insert") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
(1 ... 200).map({ _ in rndLet() }) | |
.lazy.forEach { x.insert($0) } | |
} | |
} | |
// Me | |
timetest("Erica - unlazy") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = Set((1 ... 200).map({ _ in rndLet() })) | |
} | |
} | |
Starting Test: Biappi - precalc letters | |
Ending Test: Biappi - precalc letters | |
Elapsed time: 0.178269895986887 | |
Starting Test: Erica - precalc letters, initializer | |
Ending Test: Erica - precalc letters, initializer | |
Elapsed time: 0.112385187007021 | |
Starting Test: Erica - precalc letters, insertion | |
Ending Test: Erica - precalc letters, insertion | |
Elapsed time: 0.0728352040168829 | |
Starting Test: Biappi | |
Ending Test: Biappi | |
Elapsed time: 1.58525137900142 | |
Starting Test: Erica - Insert | |
Ending Test: Erica - Insert | |
Elapsed time: 1.49860463797813 | |
Starting Test: Erica - unlazy | |
Ending Test: Erica - unlazy | |
Elapsed time: 1.52196816398646 | |
Program ended with exit code: 0 | |
---------------------- | |
import Foundation | |
/// Prints the elapsed time to execute a block under whatever optimization | |
/// conditions are currently in use by the compiler | |
public func timetest(_ note: String, block: () -> Void) { | |
print("Starting Test:", note) | |
let now = ProcessInfo().systemUptime | |
block() | |
let timeInterval = ProcessInfo().systemUptime - now | |
print("Ending Test:", note); print("Elapsed time: \(timeInterval)") | |
} | |
func rndLet() -> String { | |
func rnd(n: Int) -> Int { return Int(arc4random_uniform(UInt32(n))) } | |
let alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
let startIdx = alpha.index(alpha.startIndex, offsetBy: rnd(n: 26)) | |
return alpha | |
.substring(to: alpha.index(after: startIdx)) | |
.substring(from: startIdx) | |
} | |
let letters = (1 ... 1000).map({ _ in rndLet() }) | |
timetest("initializer") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = Set(letters) | |
} | |
} | |
timetest("union") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = x.union(letters) | |
} | |
} | |
timetest("insert") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = [] | |
letters.forEach ({ x.insert($0) }) | |
} | |
} | |
timetest("reduce") { | |
(1 ... 5_000).forEach { _ in | |
var x = letters.reduce(Set<String>()) { | |
$0.union(CollectionOfOne($1)) | |
} | |
} | |
} | |
print("-------------") | |
timetest("initializer") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = Set((1 ... 200) | |
.lazy.map({ _ in rndLet() })) | |
} | |
} | |
timetest("insert") { | |
var x: Set<String> = [] | |
(1 ... 5_000).forEach { _ in | |
x = [] | |
(1 ... 200).lazy.map({ _ in rndLet() }) | |
.lazy.forEach { x.insert($0) } | |
} | |
} | |
timetest("formUnion w/ array") { | |
(1 ... 5_000).forEach { _ in | |
var x: Set<String> = [] | |
(1 ... 200).lazy.map({ _ in rndLet() }) | |
.lazy.forEach { x.formUnion([$0]) } | |
} | |
} | |
timetest("reduce / union") { | |
(1 ... 5_000).forEach { _ in | |
let x: Set<String> = | |
(1 ... 200).lazy.map({ _ in rndLet() }) | |
.lazy.reduce(Set<String>()) { | |
$0.union(CollectionOfOne($1)) | |
} | |
} | |
} | |
Starting Test: initializer | |
Ending Test: initializer | |
Elapsed time: 0.652348856034223 | |
Starting Test: union | |
Ending Test: union | |
Elapsed time: 0.524669112986885 | |
Starting Test: insert | |
Ending Test: insert | |
Elapsed time: 0.572339564969297 | |
Starting Test: reduce | |
Ending Test: reduce | |
Elapsed time: 0.762973523989785 | |
------------- | |
Starting Test: initializer | |
Ending Test: initializer | |
Elapsed time: 2.57511113095097 | |
Starting Test: insert | |
Ending Test: insert | |
Elapsed time: 2.98239751899382 | |
Starting Test: formUnion w/ array | |
Ending Test: formUnion w/ array | |
Elapsed time: 2.60572798998328 | |
Starting Test: reduce / union | |
Ending Test: reduce / union | |
Elapsed time: 2.58965437801089 | |
Program ended with exit code: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment