bench(loop,factorial,10000000,45)=567ms
10000000 loops in 567ms.
Can your Haskell do this?
function factorial(x){for(var i=x-1;i>0;i--){x*=i};return x}
function bench(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){var d=new Date();var std=d.getTime();var res=x(y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10);var d2=new Date();var std2=d2.getTime();
var diff=std2-std;return diff}
function loop(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){for(var i=y-1;i>-1;i--){var res=x(z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10)}}
>>26
Its archives posts from 3 years ago.
It has more programming topics then /g/(/g/ Quality is down recently).
Its threads do not expire and easier to track.
I don't understand >>34
Its summing integers inline? or there some subtle context?
Name:
FrozenVoid!FrOzEn2BUo2009-01-05 13:45
>>34
Its seems to calculate sum of excel cells. waste of variables.
bench() doesn't use arguments array because the code would
include a slower for(;;) and conditional which would made the benchmarks slower.
It needs to pass these 10 arguments millions of times.
Speed in benchmarking is critical.
Without z1-z10 ,parsing arguments array 10 millions times would be way slower then my optimized functions, completely ruining the purpose of bench().
>>37
The stack is an "array" too, it requires memory access unless you pass arguments as registers, that said, passing 10 arguments directly will use the stack on a x86, as you can only pass 2-3 arguments as registers, so you implementation would be either slower or have similar speed to one using an args array. If you really cared about speed of your benchmarking function, you would have coded it in asm, and used the CPU instruction which returns ticks, if such a thing exists on your platform( rdtsc instruction for x86), and probably would have inlined the entire benchmark function too.
Name:
FrozenVoid!FrOzEn2BUo2009-01-05 14:04
I'm interested in programming in assembler.
However there certain features (like size of code),memory management and some opcodes i can't understand.
>>38
You'll have to be more specific with your questions, I don't understand what you mean by ``size of code".
As for memory management, if you're writting an OS, or something similar ( like a debugger which doesn't depend on the OS ), you'll have to do the memory management yourself by modifying the needed descriptors and tables, details for this can be found in the fine manuals, otherwise, you should just call whatever APIs or library functions are provided to you by the OS to do the allocation of freeing of memory. Can't answer the third question since you gave no details, but I'm sure reading the fine manuals will give you the answer.
Name:
FrozenVoid!FrOzEn2BUo2009-01-05 14:27
>>39
[size of code]
how much text would factorial+bench+loop take in asm?
I assume about 5 times. They could be more optimized but you have to maintain alot more code.
Name:
FrozenVoid!FrOzEn2BUo2009-01-05 14:33
arguments parsing routine ~10 instructions
bench+loop ~20-40 instructions
factorial would be somethign like this:
MOVE eax,x
MOVE Ebx,y
"Loop"
MUL EAX,EBX
DEC EBC
JNZ ebx,loop
then pass EAX to
print-to-scren routine
Name:
FrozenVoid!FrOzEn2BUo2009-01-05 14:47
Its just way more convenient to write in javascript(tracemonkey is near C++ speed in some case).
I'll try High-level Assembly or Linoleum assembler sometime.
>>40
depends, you can make it fairly compact with macros, but usually, asm tends to be larger than the usual high level language code, not to mention not portable, but if you do need speed or need to do low level operations, then you'll have to use it. >>42
JIT tends to be slower than code generated by a decent native code compiler, but it depends on the actual code you'll be running
Name:
FrozenVoid!FrOzEn2BUo2009-01-05 15:22
>>44
There is a use for fast Factorial or some cryptography routine(with ROR SHL and similar opcodes) but you can't embed asm in Javascript or for that matter any high-level language.
Writing in C defeats the purpose since its just asm with syntactic sugar and standard libraries.
>>47
I'm not familiar with TraceMonkey, but most widely used ENTERPRISE languages with JIT(Java, .net), offer a feature to call native libraries, in which case it should be possible, if it's not, you could just add support for it yourself if you needed it, the JIT is opensource after all.
Name:
IHBTC, AIDC2009-01-05 15:35
arguments parsing
You keep using this word. I don't think it means what you think it means.
Anyway, I've been testing an arguments/call-based version against FrozenVoid's version. I wrote a simple benchmarker that runs both versions 5 times with varying iteration counts.
It mostly showed that mine is faster on most implementations of JavaScript by a large margin, but his is actually faster on TraceMonkey by a larger margin. I'm guessing this is one of the cases where tracing JIT really shines, but FrozenVoid never actually mentioned JIT, tracing, or TraceMonkey, I naturally assumed he was referring to JS implementations in general.
PS. I tried WebKit r39553 and that was dog slow. It's supposed to have SquirrelFish extreme, but it wasn't very fast. Only twice as fast as Minefield without JIT. Maybe I did something wrong.
_______________________________________________
I think the real message here is that if you want to anything that needs predictably good performance, don't use JavaScript. Even in this extraordinarily simple case, there is no clear winner. Do we choose the elegant and mostly faster version, or the inelegant and mostly slower but in one case pretty damn fast version? Or do we switch between both dynamically, by doing retarded browser sniffing? Or do we just hope TraceMonkey will gain the ability to optimise the use of arguments and call?
_______________________________________________
Minefield 3.2a1pre, JIT off From Gecko 20090104
The result (times in ms): With 10000 iterations:
Anon: [4, 5, 5, 5, 5]
FrozenVoid: [28, 29, 29, 29, 30]
THE OUTRAGEOUSLY SHITE INTERNET EXPLORER 8 BETA 2 Notes: Had to type in results manually because it refused to let me copy the text from the page. Iterations above 10000 are hilariously inaccurate because IE8 popped up a dialog box every 0.1 fucking seconds telling me that the script was going to make the page unresponsive, so I just held the 'n' key down like a good windows luser.
Anyone have any other JS implementations to test this with? I'm interested to see results. Source code in my next post, since this post will probably hit the limit otherwise.
Note that there's two separate factorial functions. I figured this would be fairer, especially in a tracing JIT. Constructive criticism welcome.
<!DOCTYPE html>
<html>
<head><title>( ゚ ヮ゚) MITON GA SUKI!</title><meta http-equiv="Content-Type" value="text/html; encoding=utf-8"></head>
<script type="text/javascript">
function factorialAnon(x) {
for(var i = x-1; i > 0; --i)
x *= i;
return x;
}
function benchAnon(control, fun, args) {
var startDate = new Date().getTime();
control(fun, args);
var endDate = new Date().getTime();
return endDate - startDate;
}
function loopAnon(n) {
return function(f) {
for(var i = 0; i <= n; ++i) {
var res = f();
}
}
}
</script>
<script type="text/javascript">
function factorialFV(x){for(var i=x-1;i>0;i--){x*=i};return x}
function benchFV(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){var d=new Date();var std=d.getTime();var res=x(y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10);var d2=new Date();var std2=d2.getTime();
var diff=std2-std;return diff}
function loopFV(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){for(var i=y-1;i>-1;i--){var res=x(z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10)}}
</script>
function doBench() {
var res = [[], []]
var n;
try {
n = parseInt(document.form.iterations.value);
} catch(e) { n = 1000000; alert("You failed to type a proper number, so I'll just use 1000000"); }
for(var i = 0; i < 5; ++i) {
res[0].push(benchAnon(loopAnon(n), factorialAnon, [45]));
res[1].push(benchFV(loopFV, factorialFV, n, 45));
}
document.documentElement.appendChild(document.createElement("HR"));
var pre = document.createElement("PRE");
pre.appendChild(document.createTextNode("With " + n + " iterations:\n Anon: " + show(res[0]) + "\n FrozenVoid: " + show(res[1])));
document.documentElement.appendChild(pre);
}
</script>
<body>
<form name="form">
<input type="text" name="iterations" value="1000000">
<input type="button" onclick="doBench()" value="Go!">
</form>
<p>Times are in milliseconds.</p>
</body>
</html>
>>51
Epic fail.
This doesn't handle any arguments.
var res = f();
This is plain copy of my factorial:
function factorialAnon(x) {
for(var i = x-1; i > 0; --i)
x *= i;
return x;
}
Its obvious without arguments it faster.
However functions which require them will just not work.
>>53
The factorial is meant to be the same. It wouldn't be much use if a different function was tested, would it? I defined them separately because I didn't want to JS implementation to make assumptions about the function and them have those assumptions possibly broken when it's called in a different manner.
Anyway, as you spotted, I was using an incorrect version of the file. With the proper version below they perform the same (except on TraceMonkey, which you obviously aren't using anyway if you managed to get 567ms earlier). So your claim that using arguments/apply would be "way slower" is still false.
function loopAnon(n) {
return function(f, args) {
for(var i = 0; i <= n; ++i) {
var res = f.apply(null, args);
}
}
}
Name:
FrozenVoid!FrOzEn2BUo2009-01-05 16:23
>>59
I assume you wanted to pass an arguments array >>36
"bench() doesn't use arguments array because the code would
include a slower for(;;) and conditional which would made the benchmarks slower."
Also, I just noticed that you said you are using TraceMonkey. Now, if you'd said that at the start, I'd have agreed with you. It probably is much easier for TraceMonkey to optimise this.
Name:
Anonymous2009-01-05 16:27
The arguments array is this(except there is a switch/loop for building
loop(){ res=f(arg[1],arg[2],arg[3]) calls)} >>59
You didn't test it with 10 million loops.
Name:
Anonymous2009-01-06 2:09
User input and hope they aren't a mac user.
Name:
FrozenVoid!FrOzEn2BUo2009-01-06 3:24
>>59 The factorial is meant to be the same. It wouldn't be much use if a different function was tested, would it?
Then why did you copied and called it:
function factorialAnon(x) {
When you could just reuse my function.
Name:
FrozenVoid!FrOzEn2BUo2009-01-06 3:40
benchAnon(loopAnon(10000000), factorial, [45]);
Results in 15783ms vs my 567ms(about 30 times slower,and twice as much code text)
code:
function benchAnon(control, fun, args) {
var startDate = new Date().getTime();
control(fun, args);
var endDate = new Date().getTime();
return endDate - startDate;
}
function loopAnon(n) {
return function(f, args) {
for(var i = 0; i <= n; ++i) {
var res = f.apply(null, args);
}
}
}
Name:
Anonymous2009-01-06 5:10
mpfactorial and mpz_fac_ui are both better than the shit in this thread.
Name:
FrozenVoid!FrOzEn2BUo2009-01-06 5:12
>>66
there is a faster but inelegant solution(switch (0-200): return (precomputed factorial):
Name:
Anonymous2009-01-06 9:43
>>67 switch / return
Use a real look-up table. That's what the compiler will probably optimize too anyway.
Name:
FrozenVoid!FrOzEn2BUo2009-01-06 10:01
>>68
Epic fail.
bench(loop,ffact2,10000,2)=628ms which for 100 Million record=6280000 milliseconds which is 2 hours(my function does it in 2 seconds)
>>69
Move the definition of the lookup table to outside of the function. They're then similar in speed. I guess tracemonkey sucks at optimising that out.
var factr2=[0,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,121645100408832000,2432902008176640000,51090942171709440000,1.1240007277776077e+21,2.585201673888498e+22,6.204484017332394e+23,1.5511210043330984e+25,4.032914611266057e+26,1.0888869450418352e+28,3.048883446117138e+29,8.841761993739701e+30,2.652528598121911e+32,8.222838654177924e+33,2.6313083693369355e+35,8.68331761881189e+36,2.952327990396041e+38,1.0333147966386144e+40,3.719933267899013e+41,1.3763753091226346e+43,5.23022617466601e+44,2.0397882081197447e+46,8.15915283247898e+47,3.34525266131638e+49,1.4050061177528801e+51,6.041526306337384e+52,2.6582715747884495e+54,1.196222208654802e+56,5.502622159812089e+57,2.5862324151116827e+59,1.2413915592536068e+61,6.082818640342679e+62,3.0414093201713376e+64,1.5511187532873816e+66,8.06581751709439e+67,4.274883284060024e+69,2.308436973392413e+71,1.2696403353658264e+73,7.109985878048632e+74,4.052691950487723e+76,2.350561331282879e+78,1.386831185456898e+80,8.32098711274139e+81,5.075802138772246e+83,3.146997326038794e+85,1.9826083154044396e+87,1.2688693218588414e+89,8.247650592082472e+90,5.443449390774432e+92,3.6471110918188705e+94,2.48003554243683e+96,1.7112245242814127e+98,1.1978571669969892e+100,8.504785885678624e+101,6.123445837688612e+103,4.470115461512686e+105,3.307885441519387e+107,2.4809140811395404e+109,1.8854947016660506e+111,1.451830920282859e+113,1.1324281178206295e+115,8.94618213078298e+116,7.15694570462638e+118,5.797126020747369e+120,4.7536433370128435e+122,3.94552396972066e+124,3.314240134565354e+126,2.8171041143805494e+128,2.4227095383672744e+130,2.107757298379527e+132,1.854826422573984e+134,1.6507955160908465e+136,1.4857159644817605e+138,1.3520015276784033e+140,1.2438414054641305e+142,1.156772507081641e+144,1.0873661566567426e+146,1.0329978488239061e+148,9.916779348709491e+149,9.619275968248216e+151,9.426890448883248e+153,9.332621544394415e+155,9.332621544394418e+157,9.42594775983836e+159,9.614466715035125e+161,9.902900716486178e+163,1.0299016745145631e+166,1.0813967582402912e+168,1.1462805637347086e+170,1.2265202031961373e+172,1.324641819451829e+174,1.4438595832024942e+176,1.5882455415227423e+178,1.7629525510902457e+180,1.974506857221075e+182,2.2311927486598138e+184,2.543559733472186e+186,2.925093693493014e+188,3.393108684451899e+190,3.96993716080872e+192,4.6845258497542896e+194,5.574585761207606e+196,6.689502913449135e+198,8.094298525273444e+200,9.875044200833601e+202,1.2146304367025332e+205,1.506141741511141e+207,1.882677176888926e+209,2.3721732428800483e+211,3.0126600184576624e+213,3.856204823625808e+215,4.974504222477287e+217,6.466855489220473e+219,8.471580690878813e+221,1.1182486511960037e+224,1.4872707060906847e+226,1.99294274616152e+228,2.690472707318049e+230,3.6590428819525483e+232,5.0128887482749884e+234,6.917786472619482e+236,9.615723196941089e+238,1.3462012475717523e+241,1.8981437590761713e+243,2.6953641378881633e+245,3.8543707171800694e+247,5.550293832739308e+249,8.047926057471989e+251,1.1749972043909107e+254,1.72724589045464e+256,2.5563239178728637e+258,3.8089226376305687e+260,5.7133839564458575e+262,8.627209774233244e+264,1.3113358856834527e+267,2.0063439050956838e+269,3.0897696138473515e+271,4.789142901463393e+273,7.471062926282892e+275,1.1729568794264134e+278,1.8532718694937346e+280,2.946702272495036e+282,4.714723635992061e+284,7.590705053947223e+286,1.2296942187394494e+289,2.0044015765453032e+291,3.287218585534299e+293,5.423910666131583e+295,9.003691705778434e+297,1.5036165148649983e+300,2.5260757449731988e+302,4.2690680090047056e+304];
function ffact4(num){return (num<170 && num>-1)?factr2[num]: -1}
• The factorial of 0 is 1 (not that it matters)
• An array is a lookup table. http://en.wikipedia.org/wiki/Lookup_table
• Here it is about 1% slower with an array, about 5% slower with an associative array. (TraceMonkey here too)
Name:
FrozenVoid!FrOzEn2BUo2009-01-06 12:10
>>74
a Lookup table" a data structure, usually an array or associative array, often used to replace a runtime computation with a simpler array indexing operation".
its implementation of Object which contains data(which is more versatile but slower then arrays) into array.
a={a:'a',b:exit(),c:22} maps to ['a',exit(),22]
An Array:
In computer science an array[1] is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage.
Despite what http://dis.4chan.org/read/prog/1231209853/24,26
says, Objects use arrays because they are faster then any hashtables.
and Array is abstraction of
jmp x,return x.value
which is my switch statement does directly and twice as fast.
>>75 and Array is abstraction of jmp x,return x.value
GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT GET OUT
Hello. I am "Mr. Likes To Sage Threads". I do believe this is a thread in need of Sage, so I would like to sage it. That is why my name is "Mr. Likes To Sage Threads".