Search Unity

Optimizing Battery Consumption

Discussion in 'iOS and tvOS' started by r-pedra, Dec 16, 2016.

  1. r-pedra

    r-pedra

    Joined:
    Dec 4, 2015
    Posts:
    104
    Hi everyone,
    Many of our users are complaining about battery life when using our game.
    So I'm here trying to figure out, how to optimize the battery consumption.

    First, you need to know that the game is a 2D game using NGUI.
    This is an airline tycoon game.
    For my example, I will profile the game using the main screen with my account, that's a really advanced account but this way you will see figures for a real case.

    I'm running the game with xCode attached.
    Here is a screenshot of the main screen:

    For the record, the lines on the map are made with vectrosity, so they are basically meshes.(1 mesh)
    The aircrafts are made with a particle system, so each aircraft is a particle updated every seconds.

    Now, here is a screenshot of the energy impact in xCode:

    As you can see the impact is really high, doing nothing heavy.

    CPU usage is not really high as you can see.

    Memory usage is OK too.

    I was not able to profile GPU with my iPad Air, so I used an old iPhone 5 (perfs impact should be higher on this phone)

    Still not really high.

    We did a stat analysis in Unity Editor and the game does not have a really high amount of draw calls:


    The thing that is using the most the CPU is the flight calculations on the map:

    But remember that my account is an advanced account and that I have 625 flights to process.
    Here is the algorithm processing flight position during the time:
    Code (CSharp):
    1. public static WorldCoordinate Interpolate(WorldCoordinate from, WorldCoordinate to, float percent){
    2.         radFromLat = Mathf.Deg2Rad * from.north;
    3.         radFromLong = Mathf.Deg2Rad * from.east;
    4.         radToLat = Mathf.Deg2Rad * to.north;
    5.         radToLong = Mathf.Deg2Rad * to.east;
    6.         cosFromLat = Mathf.Cos(radFromLat);
    7.         cosToLat = Mathf.Cos(radToLat);
    8.         radDist = _computeDistanceInRadiansBetween(from,to);
    9.         sinRadDist = Mathf.Sin(radDist);
    10.         if(sinRadDist < 1.0E-6){
    11.             return new WorldCoordinate(from.north,from.east);
    12.         }
    13.  
    14.         a = Mathf.Sin((1 - percent) * radDist) / sinRadDist;
    15.         b = Mathf.Sin(percent * radDist) / sinRadDist;
    16.         c = a * cosFromLat * Mathf.Cos(radFromLong) + b * cosToLat * Mathf.Cos(radToLong);
    17.         d = a * cosFromLat * Mathf.Sin(radFromLong) + b * cosToLat * Mathf.Sin(radToLong);
    18.  
    19.         lat = Mathf.Rad2Deg * (Mathf.Atan2(a * Mathf.Sin(radFromLat) + b * Mathf.Sin(radToLat), Mathf.Sqrt(Mathf.Pow(c,2) + Mathf.Pow(d,2))));
    20.         lng = Mathf.Rad2Deg * (Mathf.Atan2(d,c));
    21.  
    22.         return new WorldCoordinate(lat,lng);
    23.     }
    24.  
    25.     static     float p1RadLat;
    26.     static     float p1RadLong;
    27.     static     float p2RadLat;
    28.     static     float p2RadLong;
    29.  
    30.     protected static float _computeDistanceInRadiansBetween(WorldCoordinate coord1, WorldCoordinate coord2){
    31.         p1RadLat = Mathf.Deg2Rad * (coord1.north);
    32.         p1RadLong = Mathf.Deg2Rad * (coord1.east);
    33.         p2RadLat = Mathf.Deg2Rad * (coord2.north);
    34.         p2RadLong = Mathf.Deg2Rad * (coord2.east);
    35.         return (2 * Mathf.Asin(Mathf.Sqrt(Mathf.Pow(Mathf.Sin((p1RadLat - p2RadLat) / 2), 2) + Mathf.Cos(p1RadLat) * Mathf.Cos(p2RadLat) * Mathf.Pow(Mathf.Sin((p1RadLong - p2RadLong) / 2), 2))));
    36.     }

    From what I see the problem is not from the CPU usage but is from the network usage.
    The problem is that for sync concerns the game must talk to the server very often, at each action. But we don't send large data, we just send forms. Does the network use the same amount of battery when you send a large amount of data and when you send a small amount?


    If anyone have any clue on how to decrease battery usage, or any advice, please answer.
     
  2. MangoPlay

    MangoPlay

    Joined:
    Dec 20, 2016
    Posts:
    3
    Notice your Garbage collection is very high and could be optimized. Probably not the issue, but something you could look into. Pretty easy to optimize garbage collection.

    using string.Format instead of string concatenation is a biggy
     
    Last edited: Dec 20, 2016
  3. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Posted with the wrong account, this message is just for the alerts
     
  4. r-pedra

    r-pedra

    Joined:
    Dec 4, 2015
    Posts:
    104
    Thank you, I already know this. Actually garbage is generated mainly by NGUI and only in the editor. During device runtime there are almost no garbage generated. Would you like a screen of the profiler on device?