Talk:Code Racer

From Second Life Wiki
Revision as of 16:02, 22 October 2007 by Ppaatt Lynagh (talk | contribs) (start towards running indefinitely many versions of code)
Jump to navigation Jump to search

I've gone ahead and created this Code Racer page -- I think the approach is complete enough to be worth discussing - e.g., I get insight into llGetSubString snippets. However, something fiddly is still wrong with the implementation, I guess. As you can see in the sample results, often the sums of wins exceed the scale. -- Ppaatt Lynagh 18:08, 16 October 2007 (PDT)

Ha! Those new insights were half-grounded in nonsense! Half the win counts were wrong, courtesy one typo. Fixed now, maybe. -- Ppaatt Lynagh 21:18, 16 October 2007 (PDT)

Talk:Hex suggests that we should tweak this instrument to call llGetTime rather than llGetTimestamp, in order to more accurately measure elapsed run time when time dilates. -- Ppaatt Lynagh 21:41, 19 October 2007 (PDT)

Change complete! Runs wonderfully faster too. The run time cost of the harness has dropped below two milliseconds. -- Ppaatt Lynagh 07:32, 20 October 2007 (PDT)

This should run indefinitely many versions of code against each other in parallel, so that we can complete an analysis like comparing all the too many versions of code at hex without attending the experiment. Also the last version of code could be an empty block of code, to measure the overhead of the harness, to throw it out, like the Efficiency Tester does. -- Ppaatt Lynagh 13:14, 22 October 2007 (PDT)

Below is a start towards running indefinitely many versions of code, without bias in favour of any one. -- Ppaatt Lynagh 16:02, 22 October 2007 (PDT)

// http://wiki.secondlife.com/wiki/Code_Racer

// List every runnable version.

list runners = [0, 1];

// Count votes to place.

list votes;

// Sum the run times observed.

list sums;

// Race a few versions of code in dilated time as measured by llGetTime.

runner0()
{
}

runner1()
{
}

runner2()
{
}

runner3()
{
}

runner4()
{
}

runner5()
{
}

// Run the chosen runner once.
// Run thru an equal time choosing between runners, no matter which runner runs.

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.

list eachRunnerRun()
{
    list befores = [];
    integer runnable = llGetListLength(runners) + 1;
    integer running;
    for (running = 0; running < runnable; ++running) // 
    {
        befores += llGetTime();
        integer runner = llList2Integer(runners, running);
        run(runner);
    }
    return befores;
}

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

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

// Race the runners and return the times when each ran.

raceRunners()
{
    list runtimes = getRuntimesElseNone(eachRunnerRun());
    list sorted = llListSort(runtimes, 1, TRUE);
    ...

...