profiling usage

How do you use the profiler ?

like, in a case like that, what should I look to find where i can optimize ?

  • thanks,

    here is my function :

    function parseISODate(date) as Moment {
        // assert(date instanceOf String)
    
        // 0123456789012345678901234
        // 2011-10-17T13:00:00-07:00
        // 2011-10-17T16:30:55.000Z
        // 2011-10-17T16:30:55Z
        if (date.length() < 20) {
            return null;
        }
    
        var moment = Gregorian.moment({
            :year => date.substring( 0, 4).toNumber(),
            :month => date.substring( 5, 7).toNumber(),
            :day => date.substring( 8, 10).toNumber(),
            :hour => date.substring(11, 13).toNumber(),
            :minute => date.substring(14, 16).toNumber(),
            :second => date.substring(17, 19).toNumber()
        });
        var suffix = date.substring(19, date.length());
    
        // skip over to time zone
        var tz = 0;
        if (suffix.substring(tz, tz + 1).equals(".")) {
            while (tz < suffix.length()) {
                var first = suffix.substring(tz, tz + 1);
                if ("-+Z".find(first) != null) {
                    break;
                }
                tz++;
            }
        }
    
        if (tz >= suffix.length()) {
            // no timezone given
            return null;
        }
        var tzOffset = 0;
        if (!suffix.substring(tz, tz + 1).equals("Z")) {
            // +HH:MM
            if (suffix.length() - tz < 6) {
            return null;
        }
        tzOffset = suffix.substring(tz + 1, tz + 3).toNumber() * Gregorian.SECONDS_PER_HOUR;
        tzOffset += suffix.substring(tz + 4, tz + 6).toNumber() * Gregorian.SECONDS_PER_MINUTE;
    
        var sign = suffix.substring(tz, tz + 1);
        if (sign.equals("+")) {
            tzOffset = -tzOffset;
        } else if (sign.equals("-") && tzOffset == 0) {
            // -00:00 denotes unknown timezone
            return null;
            }
        }
        return moment.add(new Time.Duration(tzOffset));
    }
    

    I don't see what I can improve ? less string function ?

    maybe should I try to call it less time ?

  • Where do the ISO dates come from? The web, or some other function in your app? This isn't a very friendly format. Could you possibly use a different one?

  • I don't know why it takes so long, but even more interesting: why does it run so many times? Do you parse the same string again and again? Cache it! Save the last parsed date and if the new date equals to the last parsed one then don't parse it again...

  • from a JSON from a web API

  • How often do you call the function?  Only when there is new JSON data, or each time onUpdate() is called?

  • near 12 times on each onupdate. I was wandering as said, if it would be a good idea to compute it on new JSON data, to reduce usage

  • of course. If you don't have memory issues, then keep 2 objects: 1. the input string, 2. an object that holds all the results of this calculation. Whenever you need it keep using the cached result if the date equals to the saved string.

    Maybe even better to do this when you receive the result from the web. Though even there, if the same value repeats then yu waste battery but at least it's in the background process.

  • Do it in onBackgroundData() and not use memory in the background service.  Don't keep the result in Storage, but just as a variable in the app, as Storage uses the file system and is expensive. You'll probably want to do it when the app first starts (the view's initialize())