Now, I am aware that Java is slower than C
Out of curiosity, why would you think that? Maybe this was true several years ago, but I personally get extremely fast java programs once the JIT wakes up and does its job.
Its like saying a hammer is slower then a screw driver.
I've done performance testing between Java 8 and C, and I still see noticeable differences in speed between the two languages.
It's not orders of magnitude of difference, but it's still substantial enough for me to panic every single time I see performance regressions.
Even with the above though, I don't see how such a simple program could stutter. You're enabling vsync and 60x2 IntBuffer allocations per second should be no problem. Could you share some details about your machine and environment (GPU, OS, JVM version, etc)? I don't see any stuttering whatsoever on my machine.
The demo code runs stutter free for me, running on a laptop with intel card.
So there's a few things I found interesting about my tests today:
- When I first started running tests, it looked like the stuttering had stopped entirely. UNTIL.....
- .... I had let the program run a few minutes, at which point the CPU fan kicked into its highest level. At this point, the C code continued to run smoothly, while the Java code began to exhibit the stuttering again.
I would also point out that the stuttering is extremely difficult to see if you don't have both versions of the program propped up side-by-side (I made sure to keep them on the same monitor, naturally). It's no more than one or two frames at a time that get dropped.
Now for the fun part:
One thing you need to be careful in Java is buffer allocations (line 40 in your code). In C, the width/height variables are simple integers on the stack. In Java, you're effectively paying the price of off-heap malloc + IntBuffer heap allocation, on every frame, and eventually GC + off-heap cleanup for previous frames. See if moving line 40 outside the render loop eliminates the stuttering.
This seems to have worked. I actually feel kind of silly I didn't think about that, but it definitely seems like constantly creating new buffers was the problem.
Your observation that the GC might be to blame is what I think is most accurate: It wasn't /every/ frame that was slow, it was periodic, random frames that seemed slow, and I'm not observing those slowdowns anymore.
Is there a way to mark my original post as resolved, so it doesn't clutter up the forum?