Difference between revisions of "Code Racer"

From Second Life Wiki
Jump to navigation Jump to search
m (→‎See Also: link with llGetTime as well as llGetTimestamp)
(run a number of versions of code against one another, not just two -- as developed at Talk:Code Racer)
Line 5: Line 5:
'''Q: Want to see if one version of code usually runs faster than another?'''
'''Q: Want to see if one version of code usually runs faster than another?'''


'''A:''' Run your code inside a test harness such as the example code here to race both versions time and again. Along the way, declare the winners, measuring each win by the change in [[llGetTimestamp]].
'''A:''' Run your code inside a test harness such as the example code here to race two or more versions time and again. Along the way, declare the winners, measuring each win by the change in the [[llGetTime]] dilated script time.


= Sample Results =
= Sample Results =
Line 11: Line 11:
<pre>
<pre>


14256 == llGetFreeMemory at default state entry
Click Running = No to stop this script after you've seen enough ...
Click Running = No to stop this script after you've seen enough ...
2007-10-20T14:20:09.718320Z
2007-10-25T02:33:12.806088Z


0.000000 ms on average won 6 times for version 1
~4.237066 ms elapsed in version 1 to place at 1 by 8 of 10 votes
2.209222 ms on average won 5 times for version 0
~10.047700 ms elapsed in version 2 to place at 2 by 8 of 10 votes
2007-10-20T14:20:09.849289Z
2007-10-25T02:33:15.009855Z


2.773666 ms on average won 6 times for version 1
~3.089838 ms elapsed in version 1 to place at 1 by 44 of 100 votes
2.792112 ms on average won 5 times for version 0
~5.549734 ms elapsed in version 2 to place at 2 by 57 of 100 votes
2007-10-20T14:20:09.963519Z
2007-10-25T02:33:35.422763Z


0.000000 ms on average won 7 times for version 1
~1.333064 ms elapsed in version 1 to place at 1 by 55 of 100 votes
2.151334 ms on average won 4 times for version 0
~3.434490 ms elapsed in version 2 to place at 2 by 64 of 100 votes
2007-10-20T14:20:10.094450Z
2007-10-25T02:33:56.083810Z


0.000000 ms on average won 53 times for version 1
~0.760552 ms elapsed in version 1 to place at 1 by 49 of 100 votes
0.703163 ms on average won 48 times for version 0
~1.922230 ms elapsed in version 2 to place at 2 by 63 of 100 votes
2007-10-20T14:20:10.809173Z
2007-10-25T02:34:16.564139Z


0.000000 ms on average won 52 times for version 0
~1.981575 ms elapsed in version 1 to place at 1 by 481 of 1000 votes
0.395192 ms on average won 49 times for version 1
~5.013962 ms elapsed in version 2 to place at 2 by 597 of 1000 votes
2007-10-20T14:20:11.477273Z
2007-10-25T02:37:40.371221Z


0.748504 ms on average won 52 times for version 0
~1.475276 ms elapsed in version 1 to place at 1 by 498 of 1000 votes
1.155981 ms on average won 49 times for version 1
~3.177954 ms elapsed in version 2 to place at 2 by 596 of 1000 votes
2007-10-20T14:20:12.186930Z
2007-10-25T02:41:07.869815Z


0.737245 ms on average won 503 times for version 0
~0.773118 ms elapsed in version 1 to place at 1 by 467 of 1000 votes
0.800317 ms on average won 498 times for version 1
~1.858705 ms elapsed in version 2 to place at 2 by 597 of 1000 votes
2007-10-20T14:20:18.775741Z
2007-10-25T02:44:25.003959Z
</pre>
 
= Code =


0.714376 ms on average won 510 times for version 0
<pre>
1.004643 ms on average won 491 times for version 1
// Race a few version of code in dilated script time as measured by llGetTime.
2007-10-20T14:20:25.486886Z
// http://wiki.secondlife.com/wiki/Code_Racer


0.713001 ms on average won 503 times for version 1
// List every runnable version.
0.815969 ms on average won 498 times for version 0
2007-10-20T14:20:32.022393Z


0.782364 ms on average won 5017 times for version 1
list theRunners = [0, 1, 2];
0.812907 ms on average won 4984 times for version 0
2007-10-20T14:21:37.415875Z


0.776211 ms on average won 5002 times for version 0
// Count runnable versions.
0.774247 ms on average won 4999 times for version 1
2007-10-20T14:22:43.154070Z


0.748674 ms on average won 5021 times for version 0
integer theRunnable;
0.755177 ms on average won 4980 times for version 1
 
2007-10-20T14:23:49.317798Z
// Count votes to place.
</pre>
 
list theVotes;
 
// Sum the run times observed.
 
list theSums;
 
// Count the races that ran without time running backwards.
 
integer theAverageable;
 
// Race no code alongside the other code in order to measure overhead.


= Code =
runner0()
{
}


<pre>
// Race a few versions of code.
// http://wiki.secondlife.com/wiki/Code_Racer


// Race two versions of code in dilated time as measured by llGetTime.
runner1()
{
    integer spinning;
    for (spinning = 0; spinning < 25; ++spinning)
        ;
}


version0()
runner2()
{
{
    integer spinning;
    for (spinning = 0; spinning < 50; ++spinning)
        ;
}
}


version1()
runner3()
{
{
}
}


// Count the wins.
runner4()
{
}


integer wins0;
runner5()
integer wins1;
{
}


// Sum the times.
// Run the chosen runner once.
// Run thru an equal time while choosing any runner.


float tsum0;
run(integer runner)
float tsum1;
{
    if (runner == 0) { runner0(); }
    if (runner == 1) { runner1(); }
    if (runner == 2) { runner2(); }
    if (runner == 3) { runner3(); }
    if (runner == 4) { runner4(); }
    if (runner == 5) { runner5(); }
}


// Start up.
// Start up.
Line 93: Line 120:
startup()
startup()
{
{
        llOwnerSay("");
    llOwnerSay("");
 
    llOwnerSay("Click Running = No to stop this script after you've seen enough ...");
        llOwnerSay((string) llGetFreeMemory() +
    llOwnerSay(llGetTimestamp());
            " == llGetFreeMemory at default state entry");
 
        llOwnerSay("Click Running = No to stop this script after you've seen enough ...");
        llOwnerSay(llGetTimestamp());
}
}


// Measure the race instead
// Measure the race in calendar time elapsed since the minute began,
// in calendar time elapsed since the minute began,
// if called in place of llGetTime.
// if called in place of llGetTime.
//
//
Line 115: Line 137:
}
}


// Say less time wins, else odd/even wins.
// Race the runners and return the times when each ran.
// Run in llGetTime dilated time or in getTime calendar time.


integer chooseWinner(float tv0, float tv1, integer which)
list eachRunnerRun()
{
{
     if (tv0 < tv1)
     list befores = [];
    integer runnablePlus = theRunnable + 1;
    integer running;
    for (running = 0; running < runnablePlus; ++running)
     {
     {
         return 0;
         integer running = llList2Integer(theRunners, running);
        befores += llGetTime();
        run(running);
     }
     }
     else if (tv1 < tv0)
     return befores;
}
 
// Return elapsed run time per runner,
// else return [] if time ran backwards.
 
list getRuntimesElseNone(list befores)
{
    list runtimes = [];
    float before = llList2Float(befores, 0);
    integer timing;
    for (timing = 0; timing < theRunnable; ++timing)
    {
        float after = llList2Float(befores, timing + 1);
        float runtime = after - before;
        if (runtime < 0.0) return [];
        runtimes += runtime;
        before = after;
    }
    return runtimes;
}
 
// Add to a listed float.
 
list addFloat(list sums, integer index, float addition)
{
    list results = [];
    integer summable = llGetListLength(sums);
    integer summing;
    for (summing = 0; summing < summable; ++summing)
     {
     {
         return 1;
         float result = llList2Float(sums, summing);
        if (index == summing)
        {
            result += addition;
        }
        results += result;
     }
     }
     else // if (tv0 == tv1) // rare
     return results;
}
 
// Add to a listed integer.
 
list addInteger(list sums, integer index, integer addition)
{
    list results = [];
    integer summable = llGetListLength(sums);
    integer summing;
    for (summing = 0; summing < summable; ++summing)
     {
     {
         return (which & 1);
         integer result = llList2Integer(sums, summing);
        if (index == summing)
        {
            result += addition;
        }
        results += result;
     }
     }
    return results;
}
}


// Count the wins and sum the times.
// Race the runners once, vote to place, and sum run time per runner.


declareWinner(float tv0, float tv1, integer which)
runRace()
{
{


     // Count the wins.
     // Race the runners once.


     integer winner = chooseWinner(tv0, tv1, which);
     list runtimes = getRuntimesElseNone(eachRunnerRun());
    wins0 += (1 - winner);
//  llOwnerSay("[" + llDumpList2String(runtimes, ", ") + "] == runtimes");
    wins1 += winner;


     // Sum the times.
     // Sort the runtimes into places.


     tsum0 += tv0;
     list places = llListSort(runtimes, 1, TRUE); // sort least to most
    tsum1 += tv1;
    integer placeable = llGetListLength(places); // 0 or theRunnable
 
    // Sum run time per runner.
 
    integer adding;
    for (adding = 0; adding < placeable; ++adding)
    {
        integer running = llList2Integer(theRunners, adding);
        float runtime = llList2Float(runtimes, adding);
        theSums = addFloat(theSums, running, runtime);
        ++theAverageable;
 
        // Vote to place at each equal runtime.
 
        integer placing;
        for (placing = 0; placing < placeable; ++placing)
        {
            float placed = llList2Float(places, placing);
            if (runtime == placed)
            {
                integer flat = (running * placeable) + placing;
                theVotes = addInteger(theVotes, flat, 1);
            }
        }
    }
}
}


// Run one race between two versions of code.
// Start up a burst of races.


runRace(integer which)
zeroBurst()
{
{
     if (which & 1)
     theSums = theVotes = [];
    theRunnable = llGetListLength(theRunners);
    integer placing;
    for (placing = 0; placing < theRunnable; ++placing)
     {
     {
         float t0 = llGetTime();
         theSums += 0.0;
         version1();
         integer running;
         float t1 = llGetTime();
         for (running = 0; running < theRunnable; ++running)
        version0();
         {
        float t2 = llGetTime();
            theVotes += 0;
       
         }
        if (!((t0 <= t1) && (t1 <= t2))) return;
       
         declareWinner(t1 - t0, t2 - t1, which);
    }
    else
    {
        float t0 = llGetTime();
         version0();
        float t1 = llGetTime();
        version1();
        float t2 = llGetTime();
       
        if (!((t0 <= t1) && (t1 <= t2))) return;
       
        declareWinner(t2 - t1, t1 - t0, which);
     }
     }
}
}


// Report the result for one version in a burst of races.
// Report a burst of races.


reportSpeed(list averages, list wins, integer winningVersion)
reportBurst(integer scaling)
{
{
    llOwnerSay(llList2String(averages, winningVersion) +
        " ms on average won " +
        (string) llList2String(wins, winningVersion) + " times" +
        " for version " + (string) winningVersion);
}


// Report the result of a burst of races.
    // Consider each place.
 
    integer placing;
    for (placing = 0; placing < theRunnable; ++placing)
    {
        list votes = llList2ListStrided(llList2List(theVotes, placing, -1), 0, -1, theRunnable);
        integer winner = llList2Integer(llListSort(votes, 1, TRUE), -1);
 
        // Find the winner (or the winners) of that place.


reportRace(integer scale)
        integer running;
{
        for (running = 1; running < theRunnable; ++running)
    list wins = [wins0, wins1];
        {
            integer vote4place = llList2Integer(votes, running);  
            if (vote4place == winner)
            {
 
                // Describe a winner.


    list averages = [
                float summed = llList2Float(theSums, running) - llList2Float(theSums, 0);
        (1000.0 * tsum0) / scale,
                float average = 1000.0 * (summed / theAverageable);
        (1000.0 * tsum1) / scale];


    llOwnerSay("");
                llOwnerSay("~" +
    integer winningVersion = (wins0 <= wins1); // bias to 1
                    (string) average + " ms elapsed in version " + (string) running +
    reportSpeed(averages, wins, winningVersion);
                    " to place at " + (string) placing +
    reportSpeed(averages, wins, 1 - winningVersion);
                    " by " + (string) vote4place + " of " + (string) scaling + " votes"
    llOwnerSay(llGetTimestamp());
                );
            }
        }
    }
}
}


// Run several bursts of races.
// Run a few bursts of races at each scale, for indefinitely increasing scale.


raceRepeatedly(integer scale)
runRaceRepeatedly()
{
{
       
     integer scaleable = 10;
     // Repeat any measurement at least three times.
     integer repeatable = 1; // don't bother to repeat the first burst
   
     while (TRUE)
     integer repeatable;
     for (repeatable = 0; repeatable < 3; ++repeatable)
     {
     {
          
         integer repeating;
         // Run a burst of races.
         for (repeating = 0; repeating < repeatable; ++repeating)
        {
            zeroBurst();
 
            integer scaling;
            for (scaling = 0; scaling < scaleable; ++scaling)
            {
                runRace();
                theRunners = llList2List(theRunners, -1, -1) + llList2List(theRunners, 0, -2);
            }


        wins0 = wins1 = 0;
            llOwnerSay("");
        tsum0 = tsum1 = 0.0;
            reportBurst(scaling);
       
             llOwnerSay(llGetTimestamp());
        integer which;
        for (which = 0; which < scale; ++which)
        {
             runRace(which);
         }
         }
          
          
         // Resolve near ties in favour of the lesser average time.
         scaleable *= 10;
       
         repeatable = 3; // repeat most bursts several times
        wins0 += (tsum0 < tsum1);
         wins1 += (tsum1 < tsum0);
       
        // Report frequently to pacify the human operator.
       
        reportRace(wins0 + wins1 - 2);
     }
     }
}
}
Line 248: Line 344:
     {
     {
         startup();
         startup();
         integer scale = 10;
         runRaceRepeatedly();
        while (TRUE)
        {
            raceRepeatedly(scale);
            scale *= 10;
        }
     }
     }
}
}
Line 260: Line 351:
= Instructions =
= Instructions =


This instrument quickly & accurately measures the run time difference between two fragments of code. For example, the code presented here measures the zero difference between the two empty fragments of code found inside the functions named version0 and version1. To measure other code, insert the code you want to compare into the version0 and version1 functions.
This instrument quickly & accurately measures the run time difference between a number of fragments of code. For example, the code presented here measures the difference between the two fragments of code found inside the functions named runner1 and runner2. To measure other code, insert the code you want to compare into the runner1, runner2, runner3, etc. functions. See the source line that assigns [0, 1, 2] to theRunners? List all the runners you want to run there. Leave runner0 empty so that the harness correctly measures and subtracts out its own overhead.


= Alternatives & Caveats =
= Alternatives & Caveats =


This instrument compares two run times quickly, like 100X faster than the Efficiency Tester instrument. This instrument runs two fragments of code at a time, gives you immediate results and then progressively more accurate results over time, like when you slowly fetch a detailed image from the web. This instrument burns thru hugely much run time, like as much as fifty milliseconds per version raced. Providing immediate feedback and finishing 100X faster makes work proceed when the work would otherwise be too hard & boring to attract enough volunteers.  
This instrument compares run times quickly, like 100X faster than the Efficiency Tester instrument. This instrument runs a number of fragments of code at a time, gives you immediate results and then progressively more accurate results over time, like when you slowly fetch a detailed image from the web. This instrument burns thru hugely much run time, so that it can provide immediate feedback to make work proceed when the work would otherwise be too hard & boring to attract enough volunteers.  


The [[Efficiency Tester]] instrument serves a different purpose. That instrument adds accuracy to a measure of a range of observed run times in as little time as possible. By simple arithmetic, running thru 200ms for 1,000 times necessarily takes at least 200s, aka, more than 3 minutes. That instrument runs one fragment of code at a time, but then runs that fragment many many times to try and average out any distractions that may hit the server during the run. That instrument actually can measure 200ms in as little as 10 minutes, but that instrument gives you no answer at all until after 10,000 runs and no final answer until after 30,000 runs.
The [[Efficiency Tester]] instrument serves a different purpose. That instrument adds accuracy to a measure of a range of observed run times in as little time as possible. By simple arithmetic, running thru 200ms for 1,000 times necessarily takes at least 200s, aka, more than 3 minutes. That instrument runs one fragment of code at a time, but then runs that fragment many many times to try and average out any distractions that may hit the server during the run. That instrument actually can measure 200ms in as little as 10 minutes, but that instrument gives you no answer at all until after 10,000 runs and no final answer until after 30,000 runs.

Revision as of 19:47, 24 October 2007

Introduction

Q: Want to see if one version of code usually runs faster than another?

A: Run your code inside a test harness such as the example code here to race two or more versions time and again. Along the way, declare the winners, measuring each win by the change in the llGetTime dilated script time.

Sample Results


Click Running = No to stop this script after you've seen enough ...
2007-10-25T02:33:12.806088Z

~4.237066 ms elapsed in version 1 to place at 1 by 8 of 10 votes
~10.047700 ms elapsed in version 2 to place at 2 by 8 of 10 votes
2007-10-25T02:33:15.009855Z

~3.089838 ms elapsed in version 1 to place at 1 by 44 of 100 votes
~5.549734 ms elapsed in version 2 to place at 2 by 57 of 100 votes
2007-10-25T02:33:35.422763Z

~1.333064 ms elapsed in version 1 to place at 1 by 55 of 100 votes
~3.434490 ms elapsed in version 2 to place at 2 by 64 of 100 votes
2007-10-25T02:33:56.083810Z

~0.760552 ms elapsed in version 1 to place at 1 by 49 of 100 votes
~1.922230 ms elapsed in version 2 to place at 2 by 63 of 100 votes
2007-10-25T02:34:16.564139Z

~1.981575 ms elapsed in version 1 to place at 1 by 481 of 1000 votes
~5.013962 ms elapsed in version 2 to place at 2 by 597 of 1000 votes
2007-10-25T02:37:40.371221Z

~1.475276 ms elapsed in version 1 to place at 1 by 498 of 1000 votes
~3.177954 ms elapsed in version 2 to place at 2 by 596 of 1000 votes
2007-10-25T02:41:07.869815Z

~0.773118 ms elapsed in version 1 to place at 1 by 467 of 1000 votes
~1.858705 ms elapsed in version 2 to place at 2 by 597 of 1000 votes
2007-10-25T02:44:25.003959Z

Code

// Race a few version of code in dilated script time as measured by llGetTime.
// http://wiki.secondlife.com/wiki/Code_Racer

// List every runnable version.

list theRunners = [0, 1, 2];

// Count runnable versions.

integer theRunnable;

// Count votes to place.

list theVotes;

// Sum the run times observed.

list theSums;

// Count the races that ran without time running backwards.

integer theAverageable;

// Race no code alongside the other code in order to measure overhead.

runner0()
{
}

// Race a few versions of code.

runner1()
{
    integer spinning;
    for (spinning = 0; spinning < 25; ++spinning)
        ;
}

runner2()
{
    integer spinning;
    for (spinning = 0; spinning < 50; ++spinning)
        ;
}

runner3()
{
}

runner4()
{
}

runner5()
{
}

// Run the chosen runner once.
// Run thru an equal time while choosing any runner.

run(integer runner)
{
    if (runner == 0) { runner0(); }
    if (runner == 1) { runner1(); }
    if (runner == 2) { runner2(); }
    if (runner == 3) { runner3(); }
    if (runner == 4) { runner4(); }
    if (runner == 5) { runner5(); }
}

// Start up.

startup()
{
    llOwnerSay("");
    llOwnerSay("Click Running = No to stop this script after you've seen enough ...");
    llOwnerSay(llGetTimestamp());
}

// Measure the race in calendar time elapsed since the minute began,
// if called in place of llGetTime.
//
// Note: "YYYY-MM-DDThh:mm:ss.ff..fZ" = llGetTimestamp();
// Note: 17 = 0 + llStringLength("YYYY-MM-DDThh:mm:")
// Note: -2 = -1 - llStringLength("Z")

float getTime()
{
    return (float) llGetSubString(llGetTimestamp(), 17, -2); // "ss.ff..f"
}

// Race the runners and return the times when each ran.
// Run in llGetTime dilated time or in getTime calendar time.

list eachRunnerRun()
{
    list befores = [];
    integer runnablePlus = theRunnable + 1;
    integer running;
    for (running = 0; running < runnablePlus; ++running)
    {
        integer running = llList2Integer(theRunners, running);
        befores += llGetTime();
        run(running);
    }
    return befores;
}

// Return elapsed run time per runner,
// else return [] if time ran backwards.

list getRuntimesElseNone(list befores)
{
    list runtimes = [];
    float before = llList2Float(befores, 0);
    integer timing;
    for (timing = 0; timing < theRunnable; ++timing)
    {
        float after = llList2Float(befores, timing + 1);
        float runtime = after - before;
        if (runtime < 0.0) return [];
        runtimes += runtime;
        before = after;
    }
    return runtimes;
}

// Add to a listed float.

list addFloat(list sums, integer index, float addition)
{
    list results = [];
    integer summable = llGetListLength(sums);
    integer summing;
    for (summing = 0; summing < summable; ++summing)
    {
        float result = llList2Float(sums, summing);
        if (index == summing)
        {
            result += addition;
        }
        results += result;
    }
    return results;
}

// Add to a listed integer.

list addInteger(list sums, integer index, integer addition)
{
    list results = [];
    integer summable = llGetListLength(sums);
    integer summing;
    for (summing = 0; summing < summable; ++summing)
    {
        integer result = llList2Integer(sums, summing);
        if (index == summing)
        {
            result += addition;
        }
        results += result;
    }
    return results;
}

// Race the runners once, vote to place, and sum run time per runner.

runRace()
{

    // Race the runners once.

    list runtimes = getRuntimesElseNone(eachRunnerRun());
//  llOwnerSay("[" + llDumpList2String(runtimes, ", ") + "] == runtimes");

    // Sort the runtimes into places.

    list places = llListSort(runtimes, 1, TRUE); // sort least to most
    integer placeable = llGetListLength(places); // 0 or theRunnable

    // Sum run time per runner.

    integer adding;
    for (adding = 0; adding < placeable; ++adding)
    {
        integer running = llList2Integer(theRunners, adding);
        float runtime = llList2Float(runtimes, adding);
        theSums = addFloat(theSums, running, runtime);
        ++theAverageable;

        // Vote to place at each equal runtime.

        integer placing;
        for (placing = 0; placing < placeable; ++placing)
        {
            float placed = llList2Float(places, placing);
            if (runtime == placed)
            {
                integer flat = (running * placeable) + placing;
                theVotes = addInteger(theVotes, flat, 1);
            }
        }
    }
}

// Start up a burst of races.

zeroBurst()
{
    theSums = theVotes = [];
    theRunnable = llGetListLength(theRunners);
    integer placing;
    for (placing = 0; placing < theRunnable; ++placing)
    {
        theSums += 0.0;
        integer running;
        for (running = 0; running < theRunnable; ++running)
        {
            theVotes += 0;
        }
    }
}

// Report a burst of races.

reportBurst(integer scaling)
{

    // Consider each place.

    integer placing;
    for (placing = 0; placing < theRunnable; ++placing)
    {
        list votes = llList2ListStrided(llList2List(theVotes, placing, -1), 0, -1, theRunnable);
        integer winner = llList2Integer(llListSort(votes, 1, TRUE), -1);

        // Find the winner (or the winners) of that place.

        integer running;
        for (running = 1; running < theRunnable; ++running)
        {
            integer vote4place = llList2Integer(votes, running); 
            if (vote4place == winner)
            {

                // Describe a winner.

                float summed = llList2Float(theSums, running) - llList2Float(theSums, 0);
                float average = 1000.0 * (summed / theAverageable);

                llOwnerSay("~" +
                    (string) average + " ms elapsed in version " + (string) running +
                    " to place at " + (string) placing +
                    " by " + (string) vote4place + " of " + (string) scaling + " votes"
                );
            }
        }
    }
}

// Run a few bursts of races at each scale, for indefinitely increasing scale.

runRaceRepeatedly()
{
    integer scaleable = 10;
    integer repeatable = 1; // don't bother to repeat the first burst
    while (TRUE)
    {
        integer repeating;
        for (repeating = 0; repeating < repeatable; ++repeating)
        {
            zeroBurst();

            integer scaling;
            for (scaling = 0; scaling < scaleable; ++scaling)
            {
                runRace();
                theRunners = llList2List(theRunners, -1, -1) + llList2List(theRunners, 0, -2);
            }

            llOwnerSay("");
            reportBurst(scaling);
            llOwnerSay(llGetTimestamp());
        }
        
        scaleable *= 10;
        repeatable = 3; // repeat most bursts several times
    }
}

// Race to measure relative run-time cost of multiple versions of code.
// Produce unreasonable answers when the run-time cost measured equals or exceeds 60 seconds.

default
{
    state_entry()
    {
        startup();
        runRaceRepeatedly();
    }
}

Instructions

This instrument quickly & accurately measures the run time difference between a number of fragments of code. For example, the code presented here measures the difference between the two fragments of code found inside the functions named runner1 and runner2. To measure other code, insert the code you want to compare into the runner1, runner2, runner3, etc. functions. See the source line that assigns [0, 1, 2] to theRunners? List all the runners you want to run there. Leave runner0 empty so that the harness correctly measures and subtracts out its own overhead.

Alternatives & Caveats

This instrument compares run times quickly, like 100X faster than the Efficiency Tester instrument. This instrument runs a number of fragments of code at a time, gives you immediate results and then progressively more accurate results over time, like when you slowly fetch a detailed image from the web. This instrument burns thru hugely much run time, so that it can provide immediate feedback to make work proceed when the work would otherwise be too hard & boring to attract enough volunteers.

The Efficiency Tester instrument serves a different purpose. That instrument adds accuracy to a measure of a range of observed run times in as little time as possible. By simple arithmetic, running thru 200ms for 1,000 times necessarily takes at least 200s, aka, more than 3 minutes. That instrument runs one fragment of code at a time, but then runs that fragment many many times to try and average out any distractions that may hit the server during the run. That instrument actually can measure 200ms in as little as 10 minutes, but that instrument gives you no answer at all until after 10,000 runs and no final answer until after 30,000 runs.

See the LSL Script Efficiency article for much discussion of the Efficiency Tester instrument, including recommendations on how to avoid distracting the server into spending run time running other code in parallel. Those same recommendation apply to any llGetTimestamp harness, including this instrument.

Please do try to find deserted places to run such benchmarks and remember to turn them off when you finish! Else naturally you'll be rudely lagging the sim for the other people sharing the sim with you, for however long you run the benchmark.

See Also

Functions

llGetTime - fetch the (often zeroed) dilated time in seconds and fractional seconds

llGetTimestamp - fetch the ISO 8601 "YYYY-MM-DDThh:mm:ss.ff..fZ" string that names the calendar date and time that is now

Scripts

Code Sizer - count bytes of code with perfect accuracy

Efficiency Tester - run as long as you please to count approximate milliseconds of run time with every more accuracy