the worse thing for this kind of tasks is huge memory leak on array operations. we see that just mxs implementation is not too bad. the performance is OK. ~1 sec for 1000000 elements. but any msx algorithm dies with 10000000. and there is nothing to do with it
It’s not a fair judgement to make. the dotnet variant also uses up memory, it is just not part of the heap… check the 3dsmax process memory usage.
edit: naturally, the dotnet implementation is faster and uses less memory. But I can run the mxs algorithm with 10,000,000. It just takes up 500mb of memory and 30 seconds.
usually you don’t an algorithm for itself. you heed this memory for other script operations (lets say face selection). mxs can’t use process memory, so… c# the only way to do it.
ok I give up, I’ve been trying to do this, by looking at wikipedia and by looking at your code, but can someone pleaseeeeeeeeee translate the fisher yates algorithm to a mxs only (no dotnet) version ? Thank you
ok, got it… i think, my math is so bad and I dont do c# only VB
(
print "V10"
gc()
mem = heapfree
start = timestamp()
seed(timestamp())
asize = 1000000
randoms = #()
randoms[asize]=0
randoms[1] = 1
for i = 2 to asize do
(
j = random 1 i
randoms[i] = randoms[j]
randoms[j] = i
)
end = timestamp()
format "processing: % seconds heap: %
" ((end - start) / 1000.0) (mem-heapfree)
format "Num unique elem %
" (randoms as bitarray).numberset
)
Results:
"V10"
processing: 1.815 seconds heap: 56021480L
Num unique elem 1000000
Biddle algorithm (thank you wikipedia):
Time: 0.032s, Mem: 0Kb
OK
denisT code:
Time: 0.183s, Mem: 0Kb
OK
This works:
fn myCreateArrAssembly =
(
source = @"using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace RandomNumberGenerator
{
public class RandomNumberGenerator
{
public static int [ ] CreateRandomInts ( int count )
{
//Stopwatch sw = new Stopwatch ( );
//sw.Start ( );
int [ ] arr = new int [ count ];
for ( int i = 0; i < count; ++i )
arr [ i ] = i;
var arr2 = Shuffle ( arr );
//sw.Stop ( );
//Console.WriteLine ( sw.ElapsedMilliseconds / 1000.0 );
return arr2;
}
public static T [ ] Shuffle<T> ( T [ ] array )
{
Random rnd = new Random ( );
T [ ] newArray = new T [ array.Length ];
array.CopyTo ( newArray, 0 );
for ( int i = 0; i < array.Length; ++i )
{
int index = rnd.Next ( i, array.Length );
if ( index != i )
{
T temp = newArray [ i ];
newArray [ i ] = newArray [ index ];
newArray [ index ] = temp;
}
}
return newArray;
}
}
}"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.AddRange #("System.dll")
compilerParams.GenerateInMemory = on
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
if (compilerResults.Errors.Count > 0 ) then
(
errs = stringstream ""
for i = 0 to (compilerResults.Errors.Count-1) do
(
err = compilerResults.Errors.Item[i]
format "Error:% Line:% Column:% %
" err.ErrorNumber err.Line \
err.Column err.ErrorText to:errs
)
MessageBox (errs as string) title: "Errors encountered while compiling C# code"
format "%
" errs
return undefined
)
assembly = compilerResults.CompiledAssembly
assembly.CreateInstance "RandomNumberGenerator.RandomNumberGenerator"
)
global myArrayClass = myCreateArrAssembly()
fn myrandomIndexes count =
(
myArrayClass.CreateRandomInts count asdotnetobject:true
)
(
local s = Timestamp()
myrandomIndexes 1000000
((Timestamp()) - s) / 1000.0
)
Light
it’s not working for me… what max version do you use?
my is 2010-64b. it doesn’t compile.
I tried with 2010 32bit, 2011 64bit, 2012 64bit, all worked.
What errors are you guys getting?
Light
Ah, I changed var to int[], now your code works here Light.
This may be somewhat related to the version(s) you have installed, perhaps newer installations having a somewhat positive impact on older installations, similar to the ActiveX to dotNET transition.
We’re an exclusive 2010 house, though that’s mainly just due to timeframe.
Awesome man (: Max 2010 doesn’t seem to like .NET 3.5 stuff, out of the box.
Trying to upgrade to 2012 here (:
Cheers,
Light
Update for the poll results:
I’ll be posting the new “contest” based on what the poll results are in the morning sometime (7-8 EST) unless anyone has any objections to that.
As a side note, this mini-challenge makes me regret not buying this book,
http://www.amazon.com/Algorithms-in-a-Nutshell-ebook/dp/B0043D2EGI/ref=sr_1_16?ie=UTF8&qid=1305846814&sr=8-16
the other day while at the bookstore, now I’ll definitely have to buy it.
Can we please have more mini-contests like this Denis brought up? Denis? And one other thing, it would be cool to see a pratical example of this last mini-contest we did… Denis?