Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import strutils, colors, tables, sharedtables, strformat
- import nimbench
- const
- maxColor = 0x10000
- fgPrefix = "\x1b[38;2;"
- var
- colorsCache1 = initTable[Color, string]()
- colorsCache2: TableRef[Color, string]
- colorsCache3: SharedTable[Color, string]
- colorsCache2 = newTable[Color, string]()
- colorsCache3.init
- proc getColor1(color: Color): string =
- let rgb = extractRGB(color)
- result = fgPrefix & $rgb.r & ";" & $rgb.g & ";" & $rgb.b & 'm'
- proc getColor2(color: Color): string =
- let rgb = extractRGB(color)
- result = newString(16)
- result.add(fgPrefix)
- result.add(intToStr(rgb.r))
- result.add(';')
- result.add(intToStr(rgb.g))
- result.add(';')
- result.add(intToStr(rgb.b))
- result.add('m')
- proc getColor3(color: Color): string =
- let rgb = extractRGB(color)
- result = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
- proc getColor4(color: Color): string =
- if colorsCache1.hasKey(color):
- result = colorsCache1[color]
- else:
- let rgb = extractRGB(color)
- result = fgPrefix & $rgb.r & ";" & $rgb.g & ";" & $rgb.b & 'm'
- colorsCache1[color] = result
- proc getColor5(color: Color): string =
- if colorsCache2.hasKey(color):
- result = colorsCache2[color]
- else:
- let rgb = extractRGB(color)
- result = fgPrefix & $rgb.r & ";" & $rgb.g & ";" & $rgb.b & 'm'
- colorsCache2[color] = result
- proc getColor6(color: Color): string =
- var
- s: string
- colorsCache3.withKey(color) do (c: Color, v: var string, pairExists: var bool):
- if pairExists:
- s = v
- else:
- let rgb = extractRGB(color)
- pairExists = false
- s = fgPrefix & $rgb.r & ";" & $rgb.g & ";" & $rgb.b & 'm'
- v = s
- result = s
- proc getColor7(color: Color): string =
- if colorsCache1.hasKey(color):
- result = colorsCache1[color]
- else:
- let rgb = extractRGB(color)
- colorsCache1[color] = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
- proc getColor8(color: Color): string =
- if colorsCache2.hasKey(color):
- result = colorsCache2[color]
- else:
- let rgb = extractRGB(color)
- colorsCache2[color] = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
- proc getColor9(color: Color): string =
- var
- s: string
- colorsCache3.withKey(color) do (c: Color, v: var string, pairExists: var bool):
- if pairExists:
- s = v
- else:
- let rgb = extractRGB(color)
- pairExists = false
- s = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
- v = s
- result = s
- bench(getColorSimpleConcat):
- var s: string
- for i in 0..<maxColor:
- s = getColor1(Color(i))
- doNotOptimizeAway(s)
- benchRelative(getColorConcatOfCap):
- var s: string
- for i in 0..<maxColor:
- s = getColor2(Color(i))
- doNotOptimizeAway(s)
- benchRelative(getColorFmt):
- var s: string
- for i in 0..<maxColor:
- s = getColor3(Color(i))
- doNotOptimizeAway(s)
- benchRelative(getColorFromTable):
- var s: string
- for i in 0..<maxColor:
- s = getColor4(Color(i))
- doNotOptimizeAway(s)
- benchRelative(getColorFromTableRef):
- var s: string
- for i in 0..<maxColor:
- s = getColor5(Color(i))
- doNotOptimizeAway(s)
- benchRelative(getColorFromSharedTable):
- var s: string
- for i in 0..<maxColor:
- s = getColor6(Color(i))
- doNotOptimizeAway(s)
- benchRelative(getColorFromTableFmt):
- var s: string
- for i in 0..<maxColor:
- s = getColor7(Color(i))
- doNotOptimizeAway(s)
- benchRelative(getColorFromTableRefFmt):
- var s: string
- for i in 0..<maxColor:
- s = getColor8(Color(i))
- doNotOptimizeAway(s)
- benchRelative(getColorFromSharedTableFmt):
- var s: string
- for i in 0..<maxColor:
- s = getColor9(Color(i))
- doNotOptimizeAway(s)
- runBenchmarks()
Add Comment
Please, Sign In to add comment