C backend: increase resolution of timer in gtk benchmark

This commit is contained in:
Alexey Andreev 2018-05-17 19:38:42 +03:00
parent bb3a2a22fe
commit c58e19405c
2 changed files with 19 additions and 4 deletions

View File

@ -24,6 +24,8 @@ import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.Fixture; import org.jbox2d.dynamics.Fixture;
import org.teavm.interop.Address; import org.teavm.interop.Address;
import org.teavm.interop.Function; 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.shared.Scene;
import org.teavm.samples.benchmark.teavm.gtk.Cairo; import org.teavm.samples.benchmark.teavm.gtk.Cairo;
import org.teavm.samples.benchmark.teavm.gtk.GLib; import org.teavm.samples.benchmark.teavm.gtk.GLib;
@ -114,17 +116,21 @@ public final class Gtk3BenchmarkStarter {
} }
private static int tick() { private static int tick() {
long start = System.currentTimeMillis(); long start = currentTimeNano();
scene.calculate(); scene.calculate();
long end = System.currentTimeMillis(); long end = currentTimeNano();
int second = (int) ((System.currentTimeMillis() - startMillisecond) / 1000); int second = (int) ((System.currentTimeMillis() - startMillisecond) / 1000);
if (second > currentSecond) { if (second > currentSecond) {
System.out.println("Second " + second + ": " + timeSpentCalculating + " ms"); System.out.println("Second " + second + ": " + (timeSpentCalculating / 1_000_000.0) + " ms");
timeSpentCalculating = 0; timeSpentCalculating = 0;
currentSecond = second; currentSecond = second;
} }
timeSpentCalculating += end - start; long delta = end - start;
if (delta < 0) {
delta += 1_000_000_000;
}
timeSpentCalculating += delta;
Gtk.queueDraw(canvas); Gtk.queueDraw(canvas);
GLib.delay(scene.timeUntilNextStep(), GLib.delay(scene.timeUntilNextStep(),
@ -132,4 +138,8 @@ public final class Gtk3BenchmarkStarter {
return 0; return 0;
} }
@Import(name = "currentTimeNano")
@Include(value = "support.c", isSystem = false)
private static native long currentTimeNano();
} }

View File

@ -0,0 +1,5 @@
static int64_t currentTimeNano() {
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return (int64_t) time.tv_nsec;
}