A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: What is considered CPU intensive?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Location
    Istanbul ,Turkey
    Posts
    59

    What is considered CPU intensive?

    I wanted to bring up some general purpose question for the pros
    iow developers.

    I've been hearing this for quite a time but ignored. But I guess in time every serious developer should be aware of it as things get more complex and more and more code gets runing at the same time.

    Could a general list be made if what could make a code more CPU intensive? What we should avoid or take care of to make code more simple; elegant?

    I'm not talking bout building reusable code and OOP techniq.

  2. #2
    Member
    Join Date
    Nov 2003
    Location
    Edmonton, AB
    Posts
    89
    This is a small thing, but it is much more efficient to multiply or divide things by multiples of 2 than anything else.
    I wish I could think of something clever...

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    A couple of suggestions for you.

    #1. A very common source of CPU-suckage I've seen is code that looks like this:

    code:

    // PSEUDO CODE
    onClipEvent(enterFrame)
    {
    if (needToDoSomethingSpecial)
    {
    doSomethingSpecial;
    }
    }

    on(press)
    {
    needToDoSomethingSpecial = true;
    }



    The problem with this is that if you have a lot of movieclips set up this way, and none of them are doing anything special, they are all wasting CPU with the overhead of calling the enterFrame handler. It is better to set things up like so, from either the Load handler, or from a frame script:

    code:


    somethingSpecialHandler = function()
    {
    if (doesn't need to do something special)
    {
    delete onEnterFrame;
    return;
    }
    doSomethingSpecial;
    }

    button.onPress = function()
    {
    this.onEnterFrame = somethingSpecialHandler;
    }





    #2. You can answer questions about such things as multiplication versus division yourself by performing a test. Ideally, you should perform these tests on a few different versions of flash on different platforms, but even a single test is going to give you more information than not doing the test at all.

    The key to figuring out the timing is to make a loop that repeats the test over and over again, and then timing the whole thing, like so:

    code:

    nbrTests = 100000;

    startTime = getTimer();
    for (i = 0; i < nbrTests; ++i)
    {
    // perform 1st test here
    x = i/2;
    }
    trace("method 1, elapsed: " + (getTimer() - startTime) + "ms");

    startTime = getTimer();
    for (i = 0; i < nbrTests; ++i)
    {
    // perform 2nd test here
    x = i*.5;
    }
    trace("method 2, elapsed: " + (getTimer() - startTime) + "ms");



    I have occasionally used such a test to demonstrate on this board that some carefully "optimized" code was in fact slower than the code it was intended to optimize.

    For the particular test shown above, my machine shows no difference between the two methods (if I run the test multiple times, sometimes the first method is a few ticks faster, and sometimes the second).

    The conventional wisdom is that multipling is faster, and this is typically true on older CPUs with low-level code, such as C or assembly language. In the case of Flash, there is probably so much overhead involved, that the difference between the multiplication and division is negligable.


    - Jim
    Last edited by jbum; 09-03-2004 at 05:12 PM.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center