Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Point-free programming style

Name: Anonymous 2009-01-04 19:30

How do I write the factorial function in point-free?

fact n = foldr1 (*) [1..n]

Which is faster
add1 = 1 +
or
add1 x = 1 + x
???

Name: FrozenVoid !FrOzEn2BUo 2009-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 !FrOzEn2BUo 2009-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.

Name: GAY FOR FROZENVOID 2009-01-05 15:15

>>42
buttsechs?

Name: Anonymous 2009-01-05 15:18

>>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 !FrOzEn2BUo 2009-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.

Name: Anonymous 2009-01-05 15:24

I take it you never heard of linking object files or libraries to your code.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 15:26

>>46
Currently this is not possible with Javascript.

Name: Anonymous 2009-01-05 15:28

>>45
You don't know what "high-level language" means. Please get the fuck out.

Name: Anonymous 2009-01-05 15:31

>>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, AIDC 2009-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]

With 100000 iterations:
  Anon:       [47, 47, 48, 48, 48]
  FrozenVoid: [328, 290, 313, 293, 310]

With 1000000 iterations:
  Anon:       [477, 496, 474, 474, 475]
  FrozenVoid: [3018, 3012, 3021, 3055, 3017]


Minefield 3.2a1pre, JIT on
From Gecko 20090104
With 10000 iterations:
  Anon:       [4, 4, 5, 5, 4]
  FrozenVoid: [1, 0, 0, 1, 1]

With 100000 iterations:
  Anon:       [46, 47, 46, 46, 46]
  FrozenVoid: [3, 3, 3, 3, 4]

With 1000000 iterations:
  Anon:       [459, 460, 461, 466, 467]
  FrozenVoid: [35, 33, 34, 35, 36]

 
THE ... SUPERIOR? ... JAVASCRIPT ENGINE V8
From Google® Chrome 1.0.154.36
With 10000 iterations:
  Anon:       [2, 2, 2, 3, 3]
  FrozenVoid: [17, 5, 5, 4, 4]

With 100000 iterations:
  Anon:       [26, 27, 30, 26, 25]
  FrozenVoid: [46, 50, 49, 48, 45]

With 1000000 iterations:
  Anon:       [259, 255, 255, 254, 255]
  FrozenVoid: [460, 458, 456, 461, 459]

 
SquirrelFish... XTREME?
From WebKit r39553
With 10000 iterations:
  Anon:       [2, 3, 2, 3, 2]
  FrozenVoid: [15, 15, 15, 15, 15]

With 100000 iterations:
  Anon:       [29, 27, 27, 26, 27]
  FrozenVoid: [151, 149, 150, 151, 150]

With 1000000 iterations:
  Anon:       [264, 262, 262, 262, 262]
  FrozenVoid: [1493, 1496, 1494, 1507, 1495]


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.

With that in mind:

With 10000 iterations:
  Anon:       [9, 9, 9, 9, 8]
  FrozenVoid: [68, 68, 69, 69, 69]

With 100000 iterations:
  Anon:       [89, 89, 92, 89, 88]
  FrozenVoid: [1377, 1480, 1099, 1801, 708] -- I guess at 1801 I must have got bored of holding 'n'.

With 1000000 iterations:
  Anon:       [2296, 907, 906, 905, 880]
  FrozenVoid: [7241, 7104, 7114, 7157, 7142]


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.

Name: IHBTC, AIDC 2009-01-05 15:37

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>

<script>
function show(xs) {
  return "[" + xs.join(", ") + "]"
}

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>

Name: Anonymous 2009-01-05 15:41

>>51
Wow... way to get trolled, mate.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 15:45

>>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.

Name: Anonymous 2009-01-05 15:45

>>51
Define charset before unicode content.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 15:51

If you need more details:
1. i run latest nightly firefox build.
2.the code is executed via Greasemonkey applet which loads through about:cache see http://frozenvoid.blogspot.com/2008/12/javascript-editing-utility.html

Name: Anonymous 2009-01-05 15:52

>>55
Yo you're blog suck bro.

Name: Anonymous 2009-01-05 15:54

I need details about your mother. Please post pics on your web-log.

Name: Anonymous 2009-01-05 15:54

>>56
better keep that comment count at 0

Name: Anonymous 2009-01-05 16:16

>>54 Good point.

>>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 !FrOzEn2BUo 2009-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."

Name: Anonymous 2009-01-05 16:25

>>60
That's what apply does.

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: Anonymous 2009-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: Anonymous 2009-01-06 2:09

User input and hope they aren't a mac user.

Name: FrozenVoid !FrOzEn2BUo 2009-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 !FrOzEn2BUo 2009-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: Anonymous 2009-01-06 5:10

mpfactorial and mpz_fac_ui are both better than the shit in this thread.

Name: FrozenVoid !FrOzEn2BUo 2009-01-06 5:12

>>66
there is a faster but inelegant solution(switch (0-200): return (precomputed factorial):

Name: Anonymous 2009-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 !FrOzEn2BUo 2009-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)

code:

function ffact2(num){var factr={
 0: 0, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120, 6: 720, 7: 5040, 8: 40320, 9: 362880, 10: 3628800, 11: 39916800, 12: 479001600, 13: 6227020800, 14: 87178291200, 15: 1307674368000, 16: 20922789888000, 17: 355687428096000, 18: 6402373705728000, 19: 121645100408832000, 20: 2432902008176640000, 21: 51090942171709440000, 22: 1.1240007277776077e+21, 23: 2.585201673888498e+22, 24: 6.204484017332394e+23, 25: 1.5511210043330984e+25, 26: 4.032914611266057e+26, 27: 1.0888869450418352e+28, 28: 3.048883446117138e+29, 29: 8.841761993739701e+30, 30: 2.652528598121911e+32, 31: 8.222838654177924e+33, 32: 2.6313083693369355e+35, 33: 8.68331761881189e+36, 34: 2.952327990396041e+38, 35: 1.0333147966386144e+40, 36: 3.719933267899013e+41, 37: 1.3763753091226346e+43, 38: 5.23022617466601e+44, 39: 2.0397882081197447e+46, 40: 8.15915283247898e+47, 41: 3.34525266131638e+49, 42: 1.4050061177528801e+51, 43: 6.041526306337384e+52, 44: 2.6582715747884495e+54, 45: 1.196222208654802e+56, 46: 5.502622159812089e+57, 47: 2.5862324151116827e+59, 48: 1.2413915592536068e+61, 49: 6.082818640342679e+62, 50: 3.0414093201713376e+64, 51: 1.5511187532873816e+66, 52: 8.06581751709439e+67, 53: 4.274883284060024e+69, 54: 2.308436973392413e+71, 55: 1.2696403353658264e+73, 56: 7.109985878048632e+74, 57: 4.052691950487723e+76, 58: 2.350561331282879e+78, 59: 1.386831185456898e+80, 60: 8.32098711274139e+81, 61: 5.075802138772246e+83, 62: 3.146997326038794e+85, 63: 1.9826083154044396e+87, 64: 1.2688693218588414e+89, 65: 8.247650592082472e+90, 66: 5.443449390774432e+92, 67: 3.6471110918188705e+94, 68: 2.48003554243683e+96, 69: 1.7112245242814127e+98, 70: 1.1978571669969892e+100, 71: 8.504785885678624e+101, 72: 6.123445837688612e+103, 73: 4.470115461512686e+105, 74: 3.307885441519387e+107, 75: 2.4809140811395404e+109, 76: 1.8854947016660506e+111, 77: 1.451830920282859e+113, 78: 1.1324281178206295e+115, 79: 8.94618213078298e+116, 80: 7.15694570462638e+118, 81: 5.797126020747369e+120, 82: 4.7536433370128435e+122, 83: 3.94552396972066e+124, 84: 3.314240134565354e+126, 85: 2.8171041143805494e+128, 86: 2.4227095383672744e+130, 87: 2.107757298379527e+132, 88: 1.854826422573984e+134, 89: 1.6507955160908465e+136, 90: 1.4857159644817605e+138, 91: 1.3520015276784033e+140, 92: 1.2438414054641305e+142, 93: 1.156772507081641e+144, 94: 1.0873661566567426e+146, 95: 1.0329978488239061e+148, 96: 9.916779348709491e+149, 97: 9.619275968248216e+151, 98: 9.426890448883248e+153, 99: 9.332621544394415e+155, 100: 9.332621544394418e+157, 101: 9.42594775983836e+159, 102: 9.614466715035125e+161, 103: 9.902900716486178e+163, 104: 1.0299016745145631e+166, 105: 1.0813967582402912e+168, 106: 1.1462805637347086e+170, 107: 1.2265202031961373e+172, 108: 1.324641819451829e+174, 109: 1.4438595832024942e+176, 110: 1.5882455415227423e+178, 111: 1.7629525510902457e+180, 112: 1.974506857221075e+182, 113: 2.2311927486598138e+184, 114: 2.543559733472186e+186, 115: 2.925093693493014e+188, 116: 3.393108684451899e+190, 117: 3.96993716080872e+192, 118: 4.6845258497542896e+194, 119: 5.574585761207606e+196, 120: 6.689502913449135e+198, 121: 8.094298525273444e+200, 122: 9.875044200833601e+202, 123: 1.2146304367025332e+205, 124: 1.506141741511141e+207, 125: 1.882677176888926e+209, 126: 2.3721732428800483e+211, 127: 3.0126600184576624e+213, 128: 3.856204823625808e+215, 129: 4.974504222477287e+217, 130: 6.466855489220473e+219, 131: 8.471580690878813e+221, 132: 1.1182486511960037e+224, 133: 1.4872707060906847e+226, 134: 1.99294274616152e+228, 135: 2.690472707318049e+230, 136: 3.6590428819525483e+232, 137: 5.0128887482749884e+234, 138: 6.917786472619482e+236, 139: 9.615723196941089e+238, 140: 1.3462012475717523e+241, 141: 1.8981437590761713e+243, 142: 2.6953641378881633e+245, 143: 3.8543707171800694e+247, 144: 5.550293832739308e+249, 145: 8.047926057471989e+251, 146: 1.1749972043909107e+254, 147: 1.72724589045464e+256, 148: 2.5563239178728637e+258, 149: 3.8089226376305687e+260, 150: 5.7133839564458575e+262, 151: 8.627209774233244e+264, 152: 1.3113358856834527e+267, 153: 2.0063439050956838e+269, 154: 3.0897696138473515e+271, 155: 4.789142901463393e+273,156: 7.471062926282892e+275, 157: 1.1729568794264134e+278, 158: 1.8532718694937346e+280, 159: 2.946702272495036e+282, 160: 4.714723635992061e+284, 161: 7.590705053947223e+286, 162: 1.2296942187394494e+289, 163: 2.0044015765453032e+291, 164: 3.287218585534299e+293, 165: 5.423910666131583e+295, 166: 9.003691705778434e+297, 167: 1.5036165148649983e+300, 168: 2.5260757449731988e+302, 169: 4.2690680090047056e+304};
return (num<170 && num>-1)?factr[num]: -1
}

Name: Anonymous 2009-01-06 10:29

>>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.

Name: FrozenVoid !FrOzEn2BUo 2009-01-06 10:47

>>70
Even with breaking the function encapsulation:
bench(loop,ffact3,10000000,110)=1131ms which for 100M is 11310ms vs  2136ms (5 times faster)
http://dis.4chan.org/read/prog/1231236743/1-

code:

var factr={
 0: 0, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120, 6: 720, 7: 5040, 8: 40320, 9: 362880, 10: 3628800, 11: 39916800, 12: 479001600, 13: 6227020800, 14: 87178291200, 15: 1307674368000, 16: 20922789888000, 17: 355687428096000, 18: 6402373705728000, 19: 121645100408832000, 20: 2432902008176640000, 21: 51090942171709440000, 22: 1.1240007277776077e+21, 23: 2.585201673888498e+22, 24: 6.204484017332394e+23, 25: 1.5511210043330984e+25, 26: 4.032914611266057e+26, 27: 1.0888869450418352e+28, 28: 3.048883446117138e+29, 29: 8.841761993739701e+30, 30: 2.652528598121911e+32, 31: 8.222838654177924e+33, 32: 2.6313083693369355e+35, 33: 8.68331761881189e+36, 34: 2.952327990396041e+38, 35: 1.0333147966386144e+40, 36: 3.719933267899013e+41, 37: 1.3763753091226346e+43, 38: 5.23022617466601e+44, 39: 2.0397882081197447e+46, 40: 8.15915283247898e+47, 41: 3.34525266131638e+49, 42: 1.4050061177528801e+51, 43: 6.041526306337384e+52, 44: 2.6582715747884495e+54, 45: 1.196222208654802e+56, 46: 5.502622159812089e+57, 47: 2.5862324151116827e+59, 48: 1.2413915592536068e+61, 49: 6.082818640342679e+62, 50: 3.0414093201713376e+64, 51: 1.5511187532873816e+66, 52: 8.06581751709439e+67, 53: 4.274883284060024e+69, 54: 2.308436973392413e+71, 55: 1.2696403353658264e+73, 56: 7.109985878048632e+74, 57: 4.052691950487723e+76, 58: 2.350561331282879e+78, 59: 1.386831185456898e+80, 60: 8.32098711274139e+81, 61: 5.075802138772246e+83, 62: 3.146997326038794e+85, 63: 1.9826083154044396e+87, 64: 1.2688693218588414e+89, 65: 8.247650592082472e+90, 66: 5.443449390774432e+92, 67: 3.6471110918188705e+94, 68: 2.48003554243683e+96, 69: 1.7112245242814127e+98, 70: 1.1978571669969892e+100, 71: 8.504785885678624e+101, 72: 6.123445837688612e+103, 73: 4.470115461512686e+105, 74: 3.307885441519387e+107, 75: 2.4809140811395404e+109, 76: 1.8854947016660506e+111, 77: 1.451830920282859e+113, 78: 1.1324281178206295e+115, 79: 8.94618213078298e+116, 80: 7.15694570462638e+118, 81: 5.797126020747369e+120, 82: 4.7536433370128435e+122, 83: 3.94552396972066e+124, 84: 3.314240134565354e+126, 85: 2.8171041143805494e+128, 86: 2.4227095383672744e+130, 87: 2.107757298379527e+132, 88: 1.854826422573984e+134, 89: 1.6507955160908465e+136, 90: 1.4857159644817605e+138, 91: 1.3520015276784033e+140, 92: 1.2438414054641305e+142, 93: 1.156772507081641e+144, 94: 1.0873661566567426e+146, 95: 1.0329978488239061e+148, 96: 9.916779348709491e+149, 97: 9.619275968248216e+151, 98: 9.426890448883248e+153, 99: 9.332621544394415e+155, 100: 9.332621544394418e+157, 101: 9.42594775983836e+159, 102: 9.614466715035125e+161, 103: 9.902900716486178e+163, 104: 1.0299016745145631e+166, 105: 1.0813967582402912e+168, 106: 1.1462805637347086e+170, 107: 1.2265202031961373e+172, 108: 1.324641819451829e+174, 109: 1.4438595832024942e+176, 110: 1.5882455415227423e+178, 111: 1.7629525510902457e+180, 112: 1.974506857221075e+182, 113: 2.2311927486598138e+184, 114: 2.543559733472186e+186, 115: 2.925093693493014e+188, 116: 3.393108684451899e+190, 117: 3.96993716080872e+192, 118: 4.6845258497542896e+194, 119: 5.574585761207606e+196, 120: 6.689502913449135e+198, 121: 8.094298525273444e+200, 122: 9.875044200833601e+202, 123: 1.2146304367025332e+205, 124: 1.506141741511141e+207, 125: 1.882677176888926e+209, 126: 2.3721732428800483e+211, 127: 3.0126600184576624e+213, 128: 3.856204823625808e+215, 129: 4.974504222477287e+217, 130: 6.466855489220473e+219, 131: 8.471580690878813e+221, 132: 1.1182486511960037e+224, 133: 1.4872707060906847e+226, 134: 1.99294274616152e+228, 135: 2.690472707318049e+230, 136: 3.6590428819525483e+232, 137: 5.0128887482749884e+234, 138: 6.917786472619482e+236, 139: 9.615723196941089e+238, 140: 1.3462012475717523e+241, 141: 1.8981437590761713e+243, 142: 2.6953641378881633e+245, 143: 3.8543707171800694e+247, 144: 5.550293832739308e+249, 145: 8.047926057471989e+251, 146: 1.1749972043909107e+254, 147: 1.72724589045464e+256, 148: 2.5563239178728637e+258, 149: 3.8089226376305687e+260, 150: 5.7133839564458575e+262, 151: 8.627209774233244e+264, 152: 1.3113358856834527e+267, 153: 2.0063439050956838e+269, 154: 3.0897696138473515e+271, 155: 4.789142901463393e+273,156: 7.471062926282892e+275, 157: 1.1729568794264134e+278, 158: 1.8532718694937346e+280, 159: 2.946702272495036e+282, 160: 4.714723635992061e+284, 161: 7.590705053947223e+286, 162: 1.2296942187394494e+289, 163: 2.0044015765453032e+291, 164: 3.287218585534299e+293, 165: 5.423910666131583e+295, 166: 9.003691705778434e+297, 167: 1.5036165148649983e+300, 168: 2.5260757449731988e+302, 169: 4.2690680090047056e+304};
function ffact3(num){
return (num<170 && num>-1)?factr[num]: -1
}

Name: Anonymous 2009-01-06 11:18

>>71
Use an array instead of an associative container.

Name: FrozenVoid !FrOzEn2BUo 2009-01-06 11:31

>>72
Then it would be not a "lookup table" >>68,70
 but an array( see http://dis.4chan.org/read/prog/1231209853/1-40
However even array is slower:
bench(loop,ffact4,100000000,10)= 3924ms vs 2136ms (mine about twice faster)

code:

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}

Name: Anonymous 2009-01-06 11:51

• 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 !FrOzEn2BUo 2009-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.

Name: Anonymous 2009-01-06 18:58

>>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

Name: Mr. Likes To Sage Threads 2009-01-07 0:22

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".

Name: Anonymous 2009-01-07 4:17

>>78
you have no idea what a jump is, get out of /prog/

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List