Guest User

Untitled

a guest
Nov 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import Foundation
  2.  
  3. func machToNanoseconds(_ mach: UInt64) -> Double {
  4. struct Static {
  5. static var info: mach_timebase_info = {
  6. var info = mach_timebase_info()
  7. mach_timebase_info(&info)
  8. return info
  9. }()
  10. }
  11. return Double(mach) * Double(Static.info.numer) / Double(Static.info.denom)
  12. }
  13.  
  14. func Test() {
  15. for threadCount in 1 ... 6 {
  16. let threadCountString = "* Thread count: \(threadCount) *"
  17. let surround = "".padding(toLength: threadCountString.count, withPad: "*", startingAt: 0)
  18. print(surround)
  19. print(threadCountString)
  20. print(surround)
  21.  
  22. let iterations: UInt64 = 1000000
  23. let interval = iterations / 20
  24.  
  25. var times: [UInt64] = []
  26. let cond = NSCondition()
  27.  
  28. var runningThreads = threadCount
  29. for _ in 0 ..< threadCount {
  30. Thread.detachNewThread({
  31. let oneTimes = ThreadFunc(iterations: iterations, interval: interval)
  32.  
  33. cond.lock()
  34. times.append(contentsOf: oneTimes)
  35. runningThreads -= 1
  36. cond.signal()
  37. cond.unlock()
  38. })
  39. }
  40.  
  41. cond.lock()
  42. while runningThreads > 0 {
  43. cond.wait()
  44. }
  45. cond.unlock()
  46.  
  47. let nanoseconds = times.map({ machToNanoseconds($0) })
  48.  
  49. for threshold in [0.01, 0.02, 0.03, 0.04, 0.05, 0.1] {
  50. print("Threshold: \(threshold)")
  51. let clusters = cluster(nanoseconds, threshold: threshold)
  52. for cluster in clusters {
  53. let mean = cluster.reduce(0, +) / Double(cluster.count)
  54. let median = cluster[cluster.count / 2]
  55. let stddev = sqrt(cluster.map({ ($0 - mean) * ($0 - mean) }).reduce(0, +) / Double(cluster.count))
  56.  
  57. print("count: \(cluster.count) - mean: \(mean) - median: \(median) - stddev: \(stddev)")
  58. }
  59. print("----------")
  60. }
  61.  
  62. print("")
  63. print("")
  64. }
  65.  
  66. print("********")
  67. print("* DONE *")
  68. print("********")
  69. }
  70.  
  71. func ThreadFunc(iterations: UInt64, interval: UInt64) -> [UInt64] {
  72. var times: [UInt64] = []
  73.  
  74. for i in 1 ... iterations {
  75. let start = mach_absolute_time()
  76.  
  77. let x = Int64(i)
  78. var guess = x
  79. for _ in 0 ... 1024 {
  80. guess = (guess + x / guess) / 2
  81. }
  82. if abs(guess * guess - x) > 1000000000 {
  83. print("Found a really inexact square root! \(guess * guess) \(x)")
  84. }
  85.  
  86. let end = mach_absolute_time()
  87.  
  88. if i % interval == 0 {
  89. times.append(end - start)
  90. }
  91. }
  92. return times
  93. }
  94.  
  95. func cluster(_ values: [Double], threshold: Double) -> [[Double]] {
  96. var result: [[Double]] = []
  97. var current: [Double] = []
  98.  
  99. for n in values.sorted() {
  100. if let last = current.last, (n - last) / n <= threshold {
  101. current.append(n)
  102. } else {
  103. if !current.isEmpty {
  104. result.append(current)
  105. }
  106. current = [n]
  107. }
  108. }
  109.  
  110. if !current.isEmpty {
  111. result.append(current)
  112. }
  113.  
  114. return result
  115. }
Add Comment
Please, Sign In to add comment