Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import
- math, strformat, sequtils, times
- const
- B* = 8
- Ki* = 2 ^ 10
- Mi* = Ki * Ki
- type
- CacheInfo* = object
- size*, lineSize*: int
- template incEvery*[T](arr: var openArray[T]; n, step: int) =
- for i in countup(0, n * step - 1, step):
- inc arr[i]
- template time*(s: untyped): auto =
- let t0 = cpuTime()
- s
- cpuTime() - t0
- template repeat*(count: int; s: untyped): auto =
- for i in 1 .. count:
- s
- const
- cacheInfos* = [CacheInfo(size: 32 * Ki, lineSize: 64),
- CacheInfo(size: 256 * Ki, lineSize: 64), CacheInfo(size: 2 * Mi, lineSize: 64)]
- var bytes: array[4 * Mi, byte]
- proc test(ci: CacheInfo; step, n: int) =
- let repeatCount = 50000
- let t = time(repeat(repeatCount, bytes.incEvery(n, step)))
- let T = t / n.float * 1000.0
- let b = n.float * repeatCount.float / t
- echo fmt"step={step:2}, n={n:5}: T={T:6.4f}ms, b={b/Mi.float:6.2f}MiB/s"
- proc main() =
- block:
- let ci = cacheInfos[0]
- test(ci, 1, 2 * ci.size)
- for i, ci in cacheInfos:
- echo fmt"L{i + 1}: size={ci.size div Ki}KiB, line size={ci.lineSize}B"
- test(ci, ci.lineSize, 2 * ci.size div ci.lineSize)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement