Difference between revisions of "LSL NSieve Benchmark"

From Second Life Wiki
Jump to: navigation, search
(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...)
 
m (lsl code tagging)
Line 1: Line 1:
<pre>
+
<lsl>
 
//
 
//
 
// The Great Computer Language Shootout
 
// The Great Computer Language Shootout
Line 92: Line 92:
 
     }
 
     }
 
}
 
}
</pre>
+
</lsl>

Revision as of 20:05, 13 April 2008

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

} </lsl>