Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7180501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:21:42+00:00 2026-05-28T17:21:42+00:00

I just added some computationally expensive code to an Android game I am developing.

  • 0

I just added some computationally expensive code to an Android game I am developing. The code in question is a collection of collision detection routines that get called very often (every iteration of the game-loop) and are doing a large amount of computation. I feel my collision detection implementation is fairly well developed, and as reasonably fast as I can make it in Java.

I’ve been using Traceview to profile the code, and this new piece of collision detection code has somewhat unsurprisingly doubled the duration of my game logic. That’s obviously a concern since for certain devices, this performance hit could take my game from a playable to an unplayable state.

I have been considering different ways to optimize this code, and I am wondering if by moving the code into C++ and accessing it with the JNI, if I will get some noticeable performance savings?

The above question is my main concern and my reason for asking. I’ve determined that the two following reasons would be other positive results from using the JNI. However, it is not enough to persuade me to port my code to C++.

  • This would make the code cleaner. Since most of the collision detection is some sort of vector math, it is much cleaner to be able to use overloaded operators rather than using some more verbose vector classes in Java.

  • Memory management would be simpler. Simpler you say? Well, this is a game so the garbage collector running is not welcome because the GC could end up ruining the performance of your game if it constantly has to interrupt to clean up. In C I don’t have to worry about the garbage collector, so I can avoid all the ugly things I do in Java with temporary static variables and just rely on the good old stack memory of C++

Long-winded as this question may be, I think I covered all my points. Given this information, would it be worth porting my code from Java to C++ and accessing it with the JNI (for reasons of improving performance)? Also, is there a way to measure or estimate a potential performance gain?

EDIT:

So I did it. Results? Well from TraceView’s perspective, it was a 6x increase in speed of my collision detection routine.

It wasn’t easy getting there though. Besides having to do the JNI dance, I also had to make some optimizations that I did not expect. Mainly, using a directly allocated float buffer to pass data from Java to native. My initial attempt just used a float array to hold the data in question because the conversion from Java to C++ was more natural, but that was realllly reallllly slow. The direct buffer completely side-stepped performance issues with array copying between java and native, and left me with a 6x bump.

Also, instead of rolling my own vector class, I just used the Eigen math library. I’m not sure how much of an affect this has had on performance, but at the least, it saved me the time of dev’ing my own (less efficient) vector class.

Another lesson learned is that excessive logging is bad for performance (jic that isn’t obvious).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:21:43+00:00Added an answer on May 28, 2026 at 5:21 pm

    Not really a direct answer to your question, but the following links might be of use to you:

    • Android Developers, JNI Tips.
    • Android Developers, Designing for Performance

    In the second link the following is written:

    Native code isn’t necessarily more efficient than Java. For one thing,
    there’s a cost associated with the Java-native transition, and the JIT
    can’t optimize across these boundaries. If you’re allocating native
    resources (memory on the native heap, file descriptors, or whatever),
    it can be significantly more difficult to arrange timely collection of
    these resources. You also need to compile your code for each
    architecture you wish to run on (rather than rely on it having a JIT).
    You may even have to compile multiple versions for what you consider
    the same architecture: native code compiled for the ARM processor in
    the G1 can’t take full advantage of the ARM in the Nexus One, and code
    compiled for the ARM in the Nexus One won’t run on the ARM in the G1.

    Native code is primarily useful when you have an existing native
    codebase that you want to port to Android, not for “speeding up” parts
    of a Java app.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just added some code that was taken from the Apple docs which shows
I have added some code which compiles cleanly and have just received this Windows
I just added STL usage to some code, and I'm getting this link error:
I have just compiled this code: http://www.win32developer.com/tutorial/winsock/winsock_tutorial_2.shtm I have added some codes so it
I just added an ElementHost to a windows Form(There are some other c# code
We just added an autoupdater in our software and got some bug report saying
I've just added yet another 3rd-party component to my .net project that contains a
I've added some form controls to a collection and can retrieve their properties when
Recently I've added some load-balancing capabilities to a piece of software that I wrote.
I have added some debugging code to my app which I want to call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.