×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    Game performance runs very slow in real phone

    Hi...

    When i run the java game using the emulator,the games run very well but when i run it on the real phone,the java game performance is very slow...
    Why this thing happen??

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,569

    Re: Game performance runs very slow in real phone

    The emulator reflects the development PC performance where you run the emulator. The real device does use a different Java VM implementation, diffrent processor, different memory, etc.

  3. #3
    Super Contributor
    Join Date
    Jan 2008
    Location
    Amravati, India
    Posts
    546

    Re: Game performance runs very slow in real phone

    Write proper code for displaying correct frames per second taking into account amount of time spent in processing between consecutive delay. Generally code looks like this:
    Code:
    public void run(){
        int TimeSpent = 0, StartTime;
        for(;;){
            DelayInMiliseconds = DelayNormallyRequired - TimeSpent;
            if(DelayInMiliseconds < 10){
                //Very less delay
                DelayInMiliseconds = 10;
            }
            else if(DelayInMiliseconds > 100){
                //Very much delay
                DelayInMiliseconds = 100;
            }
            WaitForApplicationExitOrTimeOut(DelayInMiliseconds);
            
            StartTime = System.currentTimeMillis();
    
            //...
            //Check for application exit and return if application wants to exit
            //...
            
            //...
            //Some processing here
            //...
            
            repaint();
            serviceRepaints();
            
            TimeSpent = System.currentTimeMillis() - StartTime;
        }
    }
    Note:
    Frames per second = 1000 / DelayNormallyRequired

    Now decrease frames per second until you get good performance on real phone.

  4. #4
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: Game performance runs very slow in real phone

    Hello achap,

    When i run the java game using the emulator,the games run very well but when i run it on the real phone,the java game performance is very slow...
    Why this thing happen??
    -Which phone are you referring to?

    -Is the game in question your own app which you're currently developing, or some other app which you downloaded for testing?

    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Game performance runs very slow in real phone

    You're experiencing a phenomenon called "PC big, phone small".

    • Your PC has a processor with 5-10 times the clock speed than that of even a fast mobile phone.
    • That processor is around 1000 times more complicated.
    • Your PC probably has 1Gb of memory, compared to the 1Mb you'll be lucky to get on most mobiles.
    • Your PC's operating system is hugely more sophisticated than that of your mobile phone.
    • The Java run-time for Windows is on the order of 100 times larger than that of a mobile phone.
    • The JVM for Windows has a highly sophisticated just-in-time compiler. Even mobile phones that have a JIT compiler have only a simple one. And many don't have such a thing at all.


    Phones also vary enormously in performance. Even the slower Windows Mobile devices (such as the HTC Touch) is around 40 times faster than the sluggish handsets (such as the Samsung Z105).

    Things you can do to optimise performance:

    Don't use callSerially()... use a thread with repaint() and serviceRepaints() as in Arpit's example. You can see my sample game code too.

    Avoid method calls. Particularly, avoid calling non-private instance methods. There is a surprisingly large overhead to this. Obviously, you have to call methods, but try to avoid calling methods in loops if they do a small amount of work. For example:

    PHP Code:
    private static String slowReverse(String s) {
        
    String reversed "";
        for (
    int i s.length() - 1>= 0i++) {
            
    reversed += s.charAt(i);
        }
        return 
    reverse;
    }

    private static 
    String fastReverse(String s) {
        
    char[] input s.toCharArray();
        
    int length input.length;
        
    char[] output = new char[length];
        for (
    int i length 1>= 0i++) {
            
    output[(length i) - 1] = input[i];
        }
        return new 
    String(output);

    String.charAt() is just a simple getter. Working with a char[] is around five times faster. You might think you don't do a lot of String processing in your game, but think about other objects that contain arrays... like Vector, for example.

    When painting, remember that every image you draw means a call to drawImage(). Draw fewer, larger images. Drawing many small images slows things down.

    Don't try to "optimise" your code by using byte or short, when int would have been fine. Inside the JVM, all arithmetic is done with ints, so forcing conversion between byte and int all the time makes the code bigger and slower.

    PHP Code:
    // don't do this!!!!
    for (byte i 0MAX_SIZEi++) {
        array[
    i]++;

    Don't do lots of work in event methods, especially keyPressed(), keyReleased(), etc. Excessive code in these methods causes slow, stuttering performance.

    Keep slow work, like RMS access, for times when a delay is acceptable.

    Consider adding timing code (as Arpit suggests) to different parts of your code, to see where the time is being spent. For example, try timing just the paint() method, or just the "update-logic" portion of the run() loop. Isolate the slow parts, so you can focus your optimisation efforts.

    Avoid API methods that you have to call many times, when another method exists that you can call once. This goes back to the "avoid calling methods" rule. For example:

    PHP Code:
    // don't do this
    private static byte[] slowRead(InputStream inint lengththrows IOException {
        
    byte[] data = new byte[length];
        for (
    int i 0lengthi++) {
            
    data[i] = in.read();
        }
        return 
    data;
    }

    // you can read a whole chunk of data in one go
    private static byte[] fastRead(InputStream inint lengththrows IOException {
        
    byte[] data = new byte[length];
        
    int amountRead 0;
        while (
    amountRead length) {
            
    amountRead += in.read(dataamountReadlength amountRead);
        }
        return 
    data;

    PHP Code:
    private static int slowMeasureChars(Font fontchar[] chars) {
        
    int width 0;
        for (
    int i 0chars.lengthi++) {
            
    width += font.charWidth(chars[i]);
        }
        return 
    width;
    }

    private static 
    int fastMeasureChars(Font fontchar[] chars) {
        return 
    font.charsWidth(chars0chars.length);

    Take advantage of methods that have native implementations. Never write code to copty the contents of an array; use System.arraycopy()!! The whole point of this method is to provide a native code implementation. It can be up to 200 times faster than copying an array in Java. String.indexOf(char) is another commonly native method.

    Cheers,
    Graham.

Similar Threads

  1. Real phone emulator
    By turnabull2 in forum OMA DM/DS/CP
    Replies: 1
    Last Post: 2006-09-11, 22:34
  2. Bring the emulator recordstore file to the real phone
    By cars2003 in forum Mobile Java General
    Replies: 6
    Last Post: 2004-11-03, 11:43
  3. Midlet runs on 7650 emulator but not on real device
    By hlehtimaki in forum Mobile Java General
    Replies: 1
    Last Post: 2002-07-15, 09:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×