module_timeit.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <time.h>
7 #include <sys/time.h>
8 
9 #include <kuroko/vm.h>
10 #include <kuroko/value.h>
11 #include <kuroko/object.h>
12 #include <kuroko/util.h>
13 
14 KRK_Function(timeit) {
15  KrkValue callable;
16  int times = 1000000;
17 
18  if (!krk_parseArgs("V|i", (const char *[]){"callable","number"},
19  &callable, &times)) {
20  return NONE_VAL();
21  }
22 
23  struct timeval tv_before, tv_after;
24  gettimeofday(&tv_before,NULL);
25  for (krk_integer_type t = 0; t < times; ++t) {
26  krk_push(callable);
27  krk_callStack(0);
28  if (unlikely(krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION)) return NONE_VAL();
29  }
30  gettimeofday(&tv_after,NULL);
31 
32  double before = (double)tv_before.tv_sec + (double)tv_before.tv_usec / 1000000.0;
33  double after = (double)tv_after.tv_sec + (double)tv_after.tv_usec / 1000000.0;
34 
35  return FLOATING_VAL(after-before);
36 }
37 
38 KRK_Module(timeit) {
39  KRK_DOC(module, "@brief Run functions very quickly without loop overhead from the interpreter.");
40 
41  BIND_FUNC(module,timeit);
42 }
KRK_Module(socket)
Struct definitions for core object types.
int flags
Definition: vm.h:165
Stack reference or primative value.
Utilities for creating native bindings.
#define krk_parseArgs(f, n,...)
Parse arguments to a function while accepting keyword arguments.
Definition: util.h:360
#define KRK_DOC(thing, text)
Attach documentation to a thing of various types.
Definition: util.h:304
Definitions for primitive stack references.
Core API for the bytecode virtual machine.
krk_threadLocal KrkThreadState krk_currentThread
Thread-local VM state.
KrkValue krk_callStack(int argCount)
Call a callable on the stack with argCount arguments.
Definition: vm.c:732
void krk_push(KrkValue value)
Push a stack value.
Definition: vm.c:118