×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    81

    the best performance...

    Hi!
    I will explain 3 situation and I wish to know how is the best in term of performance.

    There is a class A that create a large matrix of int (M[][]), and there is a class B that in something way has to mange it.

    1) M[][] is static, so B can access to it from any function calling A.M[][]

    2) M[][] is private, and is passed in the costructor of B, and a loca copy is assigned to M[][] (this.m[][] = M[][])

    3) M[][] is public, and A is passed in the costructor of B, so B can access to M[][] calling a.M[][] ) (where in the costructor of B there is: this.a = A)

    I hope that is clear..
    Many thanks!

  2. #2
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    IMHO, option 2 is the fastest.

    It is always faster to access a local var than having to access it through another function or class.

    It doesn't affect the performance on a pc too much, but on mobile devices, if you're using it heavily, there will be a performance slowdown.

    I wouldn't suggest option 3, because you are creating a whole new instance of Object A just to access array M[ ] [ ], and that wastes space.

    And it always depends on what you are trying to achieve. If you only update the var in class A at certain intervals, 2 would be quicker. If each and every time you modify the variable, you have to let class A know about it by calling another method, it might (i think) be faster to just make it static like in option 1.

    Cheers.

  3. #3
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    81
    Thank you so much, Imtang!

    I have used local (but static) variable m[][] and is it really faster!

    Any other suggestions in order to speed up my critical class? (is a class that has functions that perform a lot of calculation, so also the smallest improvement is OK)

    Tx.

  4. #4
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    Glad it worked : )

    There are multiple ways to increase code speed and reduce the size. Here are a few I can think of for now.

    Use alot less object orientation. Create as few classes and methods as possible. Each time you call a method, you incur overhead.

    So if you are only using a block of code (that resides in a method) say... only in 2 different places, just cut and paste the code where you need it and opt for the redundancy. Especially avoid calling methods within long for() loops. Move the code into the loop itself. Even something simple as having to call array.size() each time within the loop is inefficient. Store it in a local variable before entering loop.

    Avoid use of short methods (ie. get() and set() methods) if possible because the overhead of calling it takes up most of the execution time.

    Try to reuse your vars as much as possible. ie. If you are creating a new var each time within a loop, create it outside the loop and just keep reusing it. It's easier on the garbage collector.

    And of course, since you're doing multiple calculations, using the simple java optimization techniques will work.

    Here are a couple of threads with good input from the forum users about midlet optimization.

    http://discussion.forum.nokia.com/fo...threadid=24713
    http://discussion.forum.nokia.com/fo...threadid=39964

    Good Luck.

Posting Permissions

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