Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.17 KB | None | 0 0
  1. import
  2.    math, strformat, sequtils, times
  3.  
  4. const
  5.    B* = 8
  6.    Ki* = 2 ^ 10
  7.    Mi* = Ki * Ki
  8.  
  9. type
  10.    CacheInfo* = object
  11.       size*, lineSize*: int
  12.  
  13.  
  14. template incEvery*[T](arr: var openArray[T]; n, step: int) =
  15.    for i in countup(0, n * step - 1, step):
  16.       inc arr[i]
  17.  
  18. template time*(s: untyped): auto =
  19.    let t0 = cpuTime()
  20.    s
  21.    cpuTime() - t0
  22.  
  23. template repeat*(count: int; s: untyped): auto =
  24.    for i in 1 .. count:
  25.       s
  26.  
  27. const
  28.    cacheInfos* = [CacheInfo(size: 32 * Ki, lineSize: 64),
  29.       CacheInfo(size: 256 * Ki, lineSize: 64), CacheInfo(size: 2 * Mi, lineSize: 64)]
  30.  
  31. var bytes: array[4 * Mi, byte]
  32.  
  33. proc test(ci: CacheInfo; step, n: int) =
  34.    let repeatCount = 50000
  35.    let t = time(repeat(repeatCount, bytes.incEvery(n, step)))
  36.    let T = t / n.float * 1000.0
  37.    let b = n.float * repeatCount.float / t
  38.    echo fmt"step={step:2}, n={n:5}: T={T:6.4f}ms, b={b/Mi.float:6.2f}MiB/s"
  39.  
  40. proc main() =
  41.    block:
  42.       let ci = cacheInfos[0]
  43.       test(ci, 1, 2 * ci.size)
  44.    for i, ci in cacheInfos:
  45.       echo fmt"L{i + 1}: size={ci.size div Ki}KiB, line size={ci.lineSize}B"
  46.       test(ci, ci.lineSize, 2 * ci.size div ci.lineSize)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement