×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    21

    A question about effectivity

    Hi!

    I have a question about effectivity which isn't a specific J2ME issue but I will ask it anyway.

    I have som code which is executed several times every frame:
    MyObject object = (MyObject) stack1.pop();
    object.method();
    object.method2();
    stack2.push(object);

    I feels like it is unefficient to create a new object every time. An alternative is to write:
    ((MyObject) stack1.peek()).method();
    ((MyObject) stack1.peek()).method2();
    stack2.push((MyObject) stack1.pop());

    Which alternative is the fastest and most effective? Is there any other alternative which is better?
    Last edited by calleandersson; 2005-05-25 at 09:06.

  2. #2
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    IMHO, Faster of the two is method 1.

    Reason being that it takes more time to call a method and you're calling stack1.pop() thrice in method 2, it creates more overhead.

    I think it shows more in applications with less processing power like mobile phones. Not so prevalant when processing power isn't an issue.

    [edit]
    Meant to say stack1.peek() is called twice in method 2 which is not in method 1.
    [/edit]
    Last edited by lmtang; 2005-05-26 at 02:45.

  3. #3
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    check out this thread. Discusses about midlet size, efficiency.

    There is also some metion about overheads when calling methods.

    http://discussion.forum.nokia.com/fo...slow+midlet%2A

    Cheers.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    21
    Thanks for your answer

  5. #5
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280

    Re: A question about effectivity

    Originally posted by calleandersson
    Which alternative is the fastest and most effective? Is there any other alternative which is better? [/B]
    This alternative is probably better than both:
    Code:
      MyObject object = (MyObject) stack1.peek();
      object.method();
      object.method2();
    shmoove

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    21
    // smoove

    Why would that be better? I still have to pop the object from stack1 before I push it into stack2.

  7. #7
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Why? You just got a reference by using peek(), the object stays in the stack.

    shmoove

    [edit]
    Nevermind, I didn't see that there were two stacks
    [/edit]

  8. #8
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    Just a suggestion.

    If you're calling the method a whole bunch of times, instead of creating a new object everytime you execute the code, why not just create it once (say outside the loop or in the constructor), and reuse it everytime you execute the code?

    I believe it does speed things up. If I'm not mistaken, also discussed in the thread i linked to....

Posting Permissions

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