Difference between revisions of "LSL NSieve Benchmark"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<lsl>
{{LSL Header}}{{DEFAULTSORT:NSieve}}
<source lang="lsl2">
//
//
// The Great Computer Language Shootout
// The Great Computer Language Shootout
Line 92: Line 93:
     }
     }
}
}
</lsl>
</source>


== LSL2 Optimized ==
== LSL2 Optimized ==
This version was optimized for the LSL2 VM, it might run faster under Mono then the unoptimized version.
This version was optimized for the LSL2 VM, it might run faster under Mono then the unoptimized version. In LSLEditor, it runs about 20% faster.


<lsl>//
<source lang="lsl2">//
// The Great Computer Language Shootout
// The Great Computer Language Shootout
// http://shootout.alioth.debian.org/
// http://shootout.alioth.debian.org/
Line 154: Line 155:
     }
     }
}
}
</lsl>
</source>
 
{{LSLC|Library}}
{{LSLC|Benchmark}}

Latest revision as of 11:03, 25 January 2015

//
// 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();
    }
}

LSL2 Optimized

This version was optimized for the LSL2 VM, it might run faster under Mono then the unoptimized version. In LSLEditor, it runs about 20% faster.

//
// The Great Computer Language Shootout
// http://shootout.alioth.debian.org/
//
// contributed by Isaac Gouy
// modified by Babbage Linden
// optimized for LSL2 by Strife Onizuka, Apr 14 2008
//

test() 
{
    integer m = 128;

    string bytes = "11";
    integer i = 2;
    for(; i <= m; i = i << 1)
    {
        bytes += bytes;
    }
    bytes = llGetSubString(bytes , 0, m);
    integer count = 0;

    for (i = 2; i <= m; ++i)
    {
        if(llGetSubString(bytes, i, i) == "1")
        {
            integer k = i;
            while((k += i) <= m)
            {
                bytes = llInsertString(llDeleteSubString(bytes, k, k), k, "0");
            }
            ++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();
    }
}