summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorIco Doornekamp <ico@pruts.nl>2019-10-04 19:32:16 +0200
committerAndreas Rumpf <rumpf_a@web.de>2019-10-04 19:32:16 +0200
commit26676066cc4945841c9ad08e7385b1eb5578b72d (patch)
tree58e5c69c6a1cad2765aaba22b9ce29c58d64b635 /compiler
parent412011eb961cf4e5658491aa975fd9d5cd5ca916 (diff)
downloadNim-26676066cc4945841c9ad08e7385b1eb5578b72d.tar.gz
added cpuTime to VM (#12346)
* added cpuTime to VM

* Hide VM-time cpuTime() behind --benchmarkVM flag
Diffstat (limited to 'compiler')
-rw-r--r--compiler/commands.nim2
-rw-r--r--compiler/options.nim1
-rw-r--r--compiler/vmops.nim10
3 files changed, 13 insertions, 0 deletions
diff --git a/compiler/commands.nim b/compiler/commands.nim
index 386b29f8f..aaa5f6623 100644
--- a/compiler/commands.nim
+++ b/compiler/commands.nim
@@ -792,6 +792,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
       discard "the default"
     else:
       localError(conf, info, "unknown Nim version; currently supported values are: {1.0}")
+  of "benchmarkvm":
+    processOnOffSwitchG(conf, {optBenchmarkVM}, arg, pass, info)
   of "":
     conf.projectName = "-"
   else:
diff --git a/compiler/options.nim b/compiler/options.nim
index f401b1ae5..52c38e34e 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -84,6 +84,7 @@ type                          # please make sure we have under 32 options
     optNimV2
     optMultiMethods
     optNimV019
+    optBenchmarkVM            # Enables cpuTime() in the VM
 
   TGlobalOptions* = set[TGlobalOption]
 
diff --git a/compiler/vmops.nim b/compiler/vmops.nim
index a989231dd..1c184cb79 100644
--- a/compiler/vmops.nim
+++ b/compiler/vmops.nim
@@ -16,6 +16,7 @@ from math import sqrt, ln, log10, log2, exp, round, arccos, arcsin,
 from os import getEnv, existsEnv, dirExists, fileExists, putEnv, walkDir, getAppFilename
 from md5 import getMD5
 from sighashes import symBodyDigest
+from times import cpuTime
 
 from hashes import hash
 
@@ -25,6 +26,9 @@ template mathop(op) {.dirty.} =
 template osop(op) {.dirty.} =
   registerCallback(c, "stdlib.os." & astToStr(op), `op Wrapper`)
 
+template timesop(op) {.dirty.} =
+  registerCallback(c, "stdlib.times." & astToStr(op), `op Wrapper`)
+
 template systemop(op) {.dirty.} =
   registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`)
 
@@ -194,3 +198,9 @@ proc registerAdditionalOps*(c: PCtx) =
 
   registerCallback c, "stdlib.hashes.hashVmImplByte", hashVmImplByte
   registerCallback c, "stdlib.hashes.hashVmImplChar", hashVmImplByte
+
+  if optBenchmarkVM in c.config.globalOptions:
+    wrap0(cpuTime, timesop)
+  else:
+    proc cpuTime(): float = 5.391245e-44  # Randomly chosen
+    wrap0(cpuTime, timesop)