Skip to content

Instantly share code, notes, and snippets.

@goncalossilva
Created January 12, 2011 12:45
Show Gist options
  • Select an option

  • Save goncalossilva/776116 to your computer and use it in GitHub Desktop.

Select an option

Save goncalossilva/776116 to your computer and use it in GitHub Desktop.
From 53f2f7aef15c89f60a6a75b2b55cd124600d0c23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gon=C3=A7alo=20Silva?= <goncalossilva@gmail.com>
Date: Mon, 3 Jan 2011 18:51:57 +0000
Subject: [PATCH] use clock_gettime() instead of clock() when measuring process time under Linux
clock() limits it to centisecond precision on Linux
---
ext/ruby_prof/measure_process_time.h | 13 ++++++++++++-
1 files changed, 12 insertions(+), 1 deletions(-)
diff --git a/ext/ruby_prof/measure_process_time.h b/ext/ruby_prof/measure_process_time.h
index 82a9e44..e09d9fc 100644
--- a/ext/ruby_prof/measure_process_time.h
+++ b/ext/ruby_prof/measure_process_time.h
@@ -31,13 +31,24 @@
static prof_measure_t
measure_process_time()
{
- return clock(); // cpu usage (with poor precision) in linux (TODO), wall time in doze (TODO)
+#if defined(__linux__)
+ struct timespec time;
+ clock_gettime(CLOCK_MONOTONIC, &time);
+ return time.tv_sec * 1000000000 + time.tv_nsec ;
+#else
+ return clock();
+#endif
}
+
static double
convert_process_time(prof_measure_t c)
{
+#if defined(__linux__)
+ return (double) c / 1000000000;
+#else
return (double) c / CLOCKS_PER_SEC;
+#endif
}
/* Document-method: measure_process_time
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment