Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make Sys.time unboxed and noalloc
  • Loading branch information
bobot committed Nov 5, 2015
1 parent 4430692 commit 3c76d06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
15 changes: 10 additions & 5 deletions byterun/sys.c
Expand Up @@ -334,14 +334,14 @@ CAMLprim value caml_sys_system_command(value command)
CAMLreturn (Val_int(retcode));
}

CAMLprim value caml_sys_time(value unit)
double caml_sys_time_unboxed(value unit)
{
#ifdef HAS_GETRUSAGE
struct rusage ru;

getrusage (RUSAGE_SELF, &ru);
return caml_copy_double (ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1e6
+ ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1e6);
return ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1e6
+ ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1e6;
#else
#ifdef HAS_TIMES
#ifndef CLK_TCK
Expand All @@ -353,14 +353,19 @@ CAMLprim value caml_sys_time(value unit)
#endif
struct tms t;
times(&t);
return caml_copy_double((double)(t.tms_utime + t.tms_stime) / CLK_TCK);
return (double)(t.tms_utime + t.tms_stime) / CLK_TCK;
#else
/* clock() is standard ANSI C */
return caml_copy_double((double)clock() / CLOCKS_PER_SEC);
return (double)clock() / CLOCKS_PER_SEC;
#endif
#endif
}

CAMLprim value caml_sys_time(value unit)
{
return caml_copy_double(caml_sys_time_unboxed(unit));
}

#ifdef _WIN32
extern int caml_win32_random_seed (intnat data[16]);
#endif
Expand Down
3 changes: 2 additions & 1 deletion stdlib/sys.mli
Expand Up @@ -53,7 +53,8 @@ external getenv : string -> string = "caml_sys_getenv"
external command : string -> int = "caml_sys_system_command"
(** Execute the given shell command and return its exit code. *)

external time : unit -> float = "caml_sys_time"
external time : unit -> (float [@unboxed]) =
"caml_sys_time" "caml_sys_time_unboxed" [@@noalloc]
(** Return the processor time, in seconds, used by the program
since the beginning of execution. *)

Expand Down
3 changes: 2 additions & 1 deletion stdlib/sys.mlp
Expand Up @@ -44,7 +44,8 @@ external remove: string -> unit = "caml_sys_remove"
external rename : string -> string -> unit = "caml_sys_rename"
external getenv: string -> string = "caml_sys_getenv"
external command: string -> int = "caml_sys_system_command"
external time: unit -> float = "caml_sys_time"
external time: unit -> (float [@unboxed]) =
"caml_sys_time" "caml_sys_time_unboxed" [@@noalloc]
external chdir: string -> unit = "caml_sys_chdir"
external getcwd: unit -> string = "caml_sys_getcwd"
external readdir : string -> string array = "caml_sys_read_directory"
Expand Down

0 comments on commit 3c76d06

Please sign in to comment.