LSL NSieve Benchmark

From Second Life Wiki
Revision as of 16:32, 6 November 2007 by Periapse Linden (talk | contribs) (New page: <pre> // // The Great Computer Language Shootout // http://shootout.alioth.debian.org/ // // contributed by Isaac Gouy // modified by Babbage Linden // string setByteArray(integer numbyte...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
//
// The Great Computer Language Shootout
// http://shootout.alioth.debian.org/
//
// contributed by Isaac Gouy
// modified by Babbage Linden
//

string setByteArray(integer numbytes)
{
    string result = "";
    integer i;
    for(i = 0; i < numbytes; ++i)
    {
        result += "1";
    }
    result += "1";
    return result;
}

string replace(string s, integer index, string char)
{
    string result = "";
    if(index >= 1)
    { 
        result += llGetSubString(s, 0, index - 1);
    }
    result += char;
    if(index < (llStringLength(s) - 1))
    {
        result += llGetSubString(s, index + 1, -1);
    }
    return result;
}

integer get(string s, integer index)
{
    return llGetSubString(s, index, index) == "1";
}

string set(string s, integer index)
{
    return replace(s, index, "1");
}

string unset(string s, integer index)
{
    return replace(s, index, "0");
}

test() 
{
    integer m = 128;
    string bytes = setByteArray(m);
    integer count = 0;

    integer i;
    for (i=2; i <= m; i++)
    {
         if(get(bytes, i))
         {
            integer k;
            for(k=i+i; k <= m; k+=i)
            {
                bytes = unset(bytes, k);
            }
            count++;
         }
    }
    llSay(0, "Primes up to " + (string)m + " " + (string)count);
}

time()
{
    llResetTime();
    llSay(0, "Starting tests...");
    test();
    llSay(0, "Finished tests in " + (string)llGetTime() + "s");
}

default
{
    state_entry()
    {
        time();
    }
    
    touch_start(integer num)
    {
        time();
    }
}