Guest User

Untitled

a guest
Jan 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import strutils, colors, tables, sharedtables, strformat
  2. import nimbench
  3.  
  4. const
  5. maxColor = 0x10000
  6. fgPrefix = "\x1b[38;2;"
  7.  
  8. var
  9. colorsCache1 = initTable[Color, string]()
  10. colorsCache2: TableRef[Color, string]
  11. colorsCache3: SharedTable[Color, string]
  12.  
  13. colorsCache2 = newTable[Color, string]()
  14. colorsCache3.init
  15.  
  16. proc getColor1(color: Color): string =
  17. let rgb = extractRGB(color)
  18. result = fgPrefix & $rgb.r & ";" & $rgb.g & ";" & $rgb.b & 'm'
  19.  
  20. proc getColor2(color: Color): string =
  21. let rgb = extractRGB(color)
  22. result = newString(16)
  23. result.add(fgPrefix)
  24. result.add(intToStr(rgb.r))
  25. result.add(';')
  26. result.add(intToStr(rgb.g))
  27. result.add(';')
  28. result.add(intToStr(rgb.b))
  29. result.add('m')
  30.  
  31. proc getColor3(color: Color): string =
  32. let rgb = extractRGB(color)
  33. result = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
  34.  
  35. proc getColor4(color: Color): string =
  36. if colorsCache1.hasKey(color):
  37. result = colorsCache1[color]
  38. else:
  39. let rgb = extractRGB(color)
  40. result = fgPrefix & $rgb.r & ";" & $rgb.g & ";" & $rgb.b & 'm'
  41. colorsCache1[color] = result
  42.  
  43. proc getColor5(color: Color): string =
  44. if colorsCache2.hasKey(color):
  45. result = colorsCache2[color]
  46. else:
  47. let rgb = extractRGB(color)
  48. result = fgPrefix & $rgb.r & ";" & $rgb.g & ";" & $rgb.b & 'm'
  49. colorsCache2[color] = result
  50.  
  51. proc getColor6(color: Color): string =
  52. var
  53. s: string
  54.  
  55. colorsCache3.withKey(color) do (c: Color, v: var string, pairExists: var bool):
  56. if pairExists:
  57. s = v
  58. else:
  59. let rgb = extractRGB(color)
  60. pairExists = false
  61. s = fgPrefix & $rgb.r & ";" & $rgb.g & ";" & $rgb.b & 'm'
  62. v = s
  63. result = s
  64.  
  65. proc getColor7(color: Color): string =
  66. if colorsCache1.hasKey(color):
  67. result = colorsCache1[color]
  68. else:
  69. let rgb = extractRGB(color)
  70. colorsCache1[color] = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
  71.  
  72. proc getColor8(color: Color): string =
  73. if colorsCache2.hasKey(color):
  74. result = colorsCache2[color]
  75. else:
  76. let rgb = extractRGB(color)
  77. colorsCache2[color] = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
  78.  
  79. proc getColor9(color: Color): string =
  80. var
  81. s: string
  82.  
  83. colorsCache3.withKey(color) do (c: Color, v: var string, pairExists: var bool):
  84. if pairExists:
  85. s = v
  86. else:
  87. let rgb = extractRGB(color)
  88. pairExists = false
  89. s = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
  90. v = s
  91. result = s
  92.  
  93. bench(getColorSimpleConcat):
  94. var s: string
  95. for i in 0..<maxColor:
  96. s = getColor1(Color(i))
  97. doNotOptimizeAway(s)
  98.  
  99. benchRelative(getColorConcatOfCap):
  100. var s: string
  101. for i in 0..<maxColor:
  102. s = getColor2(Color(i))
  103. doNotOptimizeAway(s)
  104.  
  105. benchRelative(getColorFmt):
  106. var s: string
  107. for i in 0..<maxColor:
  108. s = getColor3(Color(i))
  109. doNotOptimizeAway(s)
  110.  
  111. benchRelative(getColorFromTable):
  112. var s: string
  113. for i in 0..<maxColor:
  114. s = getColor4(Color(i))
  115. doNotOptimizeAway(s)
  116.  
  117. benchRelative(getColorFromTableRef):
  118. var s: string
  119. for i in 0..<maxColor:
  120. s = getColor5(Color(i))
  121. doNotOptimizeAway(s)
  122.  
  123. benchRelative(getColorFromSharedTable):
  124. var s: string
  125. for i in 0..<maxColor:
  126. s = getColor6(Color(i))
  127. doNotOptimizeAway(s)
  128.  
  129. benchRelative(getColorFromTableFmt):
  130. var s: string
  131. for i in 0..<maxColor:
  132. s = getColor7(Color(i))
  133. doNotOptimizeAway(s)
  134.  
  135. benchRelative(getColorFromTableRefFmt):
  136. var s: string
  137. for i in 0..<maxColor:
  138. s = getColor8(Color(i))
  139. doNotOptimizeAway(s)
  140.  
  141. benchRelative(getColorFromSharedTableFmt):
  142. var s: string
  143. for i in 0..<maxColor:
  144. s = getColor9(Color(i))
  145. doNotOptimizeAway(s)
  146.  
  147. runBenchmarks()
Add Comment
Please, Sign In to add comment