Explorar el Código

add `time.perf_counter`

blueloveTH hace 5 meses
padre
commit
8f6ab9f436
Se han modificado 1 ficheros con 25 adiciones y 20 borrados
  1. 25 20
      src/modules/time.c

+ 25 - 20
src/modules/time.c

@@ -5,24 +5,22 @@
 #define NANOS_PER_SEC 1000000000
 #define NANOS_PER_SEC 1000000000
 
 
 #ifndef __circle__
 #ifndef __circle__
-    int64_t time_ns() {
-        struct timespec tms;
-    #ifdef CLOCK_REALTIME
-        clock_gettime(CLOCK_REALTIME, &tms);
-    #else
-        /* The C11 way */
-        timespec_get(&tms, TIME_UTC);
-    #endif
-        /* seconds, multiplied with 1 billion */
-        int64_t nanos = tms.tv_sec * (int64_t)NANOS_PER_SEC;
-        /* Add full nanoseconds */
-        nanos += tms.tv_nsec;
-        return nanos;
-    }
+int64_t time_ns() {
+    struct timespec tms;
+#ifdef CLOCK_REALTIME
+    clock_gettime(CLOCK_REALTIME, &tms);
 #else
 #else
-    int64_t time_ns() {
-        return 0;
-    }
+    /* The C11 way */
+    timespec_get(&tms, TIME_UTC);
+#endif
+    /* seconds, multiplied with 1 billion */
+    int64_t nanos = tms.tv_sec * (int64_t)NANOS_PER_SEC;
+    /* Add full nanoseconds */
+    nanos += tms.tv_nsec;
+    return nanos;
+}
+#else
+int64_t time_ns() { return 0; }
 #endif
 #endif
 
 
 static bool time_time(int argc, py_Ref argv) {
 static bool time_time(int argc, py_Ref argv) {
@@ -39,15 +37,21 @@ static bool time_time_ns(int argc, py_Ref argv) {
     return true;
     return true;
 }
 }
 
 
+static bool time_perf_counter(int argc, py_Ref argv) {
+    PY_CHECK_ARGC(0);
+    py_newfloat(py_retval(), (double)clock() / CLOCKS_PER_SEC);
+    return true;
+}
+
 static bool time_sleep(int argc, py_Ref argv) {
 static bool time_sleep(int argc, py_Ref argv) {
     PY_CHECK_ARGC(1);
     PY_CHECK_ARGC(1);
     py_f64 secs;
     py_f64 secs;
     if(!py_castfloat(argv, &secs)) return false;
     if(!py_castfloat(argv, &secs)) return false;
 
 
-    int64_t start = time_ns();
-    const int64_t end = start + secs * 1000000000;
+    clock_t start = clock();
+    const clock_t end = start + (clock_t)(secs * CLOCKS_PER_SEC);
     while(true) {
     while(true) {
-        int64_t now = time_ns();
+        clock_t now = clock();
         if(now >= end) break;
         if(now >= end) break;
     }
     }
     py_newnone(py_retval());
     py_newnone(py_retval());
@@ -101,6 +105,7 @@ void pk__add_module_time() {
 
 
     py_bindfunc(mod, "time", time_time);
     py_bindfunc(mod, "time", time_time);
     py_bindfunc(mod, "time_ns", time_time_ns);
     py_bindfunc(mod, "time_ns", time_time_ns);
+    py_bindfunc(mod, "perf_counter", time_perf_counter);
     py_bindfunc(mod, "sleep", time_sleep);
     py_bindfunc(mod, "sleep", time_sleep);
     py_bindfunc(mod, "localtime", time_localtime);
     py_bindfunc(mod, "localtime", time_localtime);
 }
 }