diff --git a/samples/benchmark/src/main/java/org/teavm/samples/benchmark/teavm/Gtk3BenchmarkStarter.java b/samples/benchmark/src/main/java/org/teavm/samples/benchmark/teavm/Gtk3BenchmarkStarter.java index 2a5497f6d..579af9bff 100644 --- a/samples/benchmark/src/main/java/org/teavm/samples/benchmark/teavm/Gtk3BenchmarkStarter.java +++ b/samples/benchmark/src/main/java/org/teavm/samples/benchmark/teavm/Gtk3BenchmarkStarter.java @@ -24,6 +24,8 @@ import org.jbox2d.dynamics.Body; import org.jbox2d.dynamics.Fixture; import org.teavm.interop.Address; import org.teavm.interop.Function; +import org.teavm.interop.Import; +import org.teavm.interop.c.Include; import org.teavm.samples.benchmark.shared.Scene; import org.teavm.samples.benchmark.teavm.gtk.Cairo; import org.teavm.samples.benchmark.teavm.gtk.GLib; @@ -114,17 +116,21 @@ public final class Gtk3BenchmarkStarter { } private static int tick() { - long start = System.currentTimeMillis(); + long start = currentTimeNano(); scene.calculate(); - long end = System.currentTimeMillis(); + long end = currentTimeNano(); int second = (int) ((System.currentTimeMillis() - startMillisecond) / 1000); if (second > currentSecond) { - System.out.println("Second " + second + ": " + timeSpentCalculating + " ms"); + System.out.println("Second " + second + ": " + (timeSpentCalculating / 1_000_000.0) + " ms"); timeSpentCalculating = 0; currentSecond = second; } - timeSpentCalculating += end - start; + long delta = end - start; + if (delta < 0) { + delta += 1_000_000_000; + } + timeSpentCalculating += delta; Gtk.queueDraw(canvas); GLib.delay(scene.timeUntilNextStep(), @@ -132,4 +138,8 @@ public final class Gtk3BenchmarkStarter { return 0; } + + @Import(name = "currentTimeNano") + @Include(value = "support.c", isSystem = false) + private static native long currentTimeNano(); } diff --git a/samples/benchmark/support.c b/samples/benchmark/support.c new file mode 100644 index 000000000..8bd234cc4 --- /dev/null +++ b/samples/benchmark/support.c @@ -0,0 +1,5 @@ +static int64_t currentTimeNano() { + struct timespec time; + clock_gettime(CLOCK_REALTIME, &time); + return (int64_t) time.tv_nsec; +}