Friday, January 13, 2012

Benchmarking arithmetic

Got curious so I decided to see how quickly and accurately some arithmetic gets done in various environments. Results below.

[ishpeck@slcatedjamu ~]$ time dc -e "16384d^p"
113837518811535904250072068341232590877598871955010488951192642300792\
621302625312821980719831387362324936714246794526258573039676337010858\
828763430793585922853616976807646165532533059489556150449708988141578\
442943112023447213983043116092849446398990980402342632274314814328953\
404819192156219054837263740293206244418473178404516135133213244063518\
784562296023799090475839668510215390134010117698262576769574042814029\
529033278052184274508131740701273566100706819850632337113494857562098\
320151217292158833266370707127916108421901433177514694034750917722728
... ....
218238270074739736264157780272832032578594361150434678340392480113080\
089703735801591153586235927851796939292373259062814173277075113674401\
829043044144045490928371875112748010711233174142910835089303346849145\
895484745236306027347877637526426501080516036812222087305078640639225\
541848209795509140539016236258676383295890570846220185368800472374864\
023572119859359115185919085597104610288583655001397080304511835993974\
191673243089264458895016889710034575213135566915400700972672256581021\
999134594756634350940758242683390117111624193719460587302044176159214\
609746781781933047509937334480699072619539011846151062186574802048674\
92382202923258637344388123757033376532121188827136

real 0m1.167s
user 0m0.283s
sys 0m0.003s



dc surprised me with its slowness until I realized it was throwing those backslashes in there. That can eat up some time.

[ishpeck@slcatedjamu ~]$ time python -c "print 16384 ** 16384"
1138375188115359042500720683412325908775988719550104889511926423007926213026253128219807198313873623249367142467945262585730396763370108588287634307935859228536169768076461655325330594895561504497089881415784429431120234472139830431160928494463989909804023426322743148143289534048191921562190548372637402932062444184731784045161351332132440635187845622960237990904758396685102153901340101176982625767695740428140295290332780521842745081317407012735661007068198506323371134948575620983201512172921588332663707071279161084219014331775146940347509177227281385333933210897869224188603686168680892745382927717262582865116798735382574696365796311299351046728260094290719606736072012259117111400667629363151930588541659099143747134721613089069740776235687657534765557725361993289329394049820885278902800491606809630123223601804037635379024208423418856532333180705506732367579643586252181430162147604245508970283612154605179099342622822975114534462339240221466716903857275803280366341678962977346051289393798425922859136969165217554566247811263669128403448140287396494805411846462566580026934215636853835525602451004242548897751439534340759663617153864418706583169642432738589331305934295186834298433853228606541975022545265261045793253180106547809428350799194427853647105017781506127477182293624210259930919849611849204841346064260162960557828534199134070897721151924
... ...
703704366990068353216089737426366069381491347684599380093222610835917961800770781409200926729603012036399122612354223297965849193568220166221823827007473973626415778027283203257859436115043467834039248011308008970373580159115358623592785179693929237325906281417327707511367440182904304414404549092837187511274801071123317414291083508930334684914589548474523630602734787763752642650108051603681222208730507864063922554184820979550914053901623625867638329589057084622018536880047237486402357211985935911518591908559710461028858365500139708030451183599397419167324308926445889501688971003457521313556691540070097267225658102199913459475663435094075824268339011711162419371946058730204417615921460974678178193304750993733448069907261953901184615106218657480204867492382202923258637344388123757033376532121188827136

real 0m0.789s
user 0m0.203s
sys 0m0.010s


Intrepid old Python has some nice optimization to help eek out those fractions of a second.

[ishpeck@slcatedjamu ~]$ time clisp -x "(expt 16384 16384)"
i i i i i i i ooooo o ooooooo ooooo ooooo
I I I I I I I 8 8 8 8 8 o 8 8
I \ `+' / I 8 8 8 8 8 8
\ `-+-' / 8 8 8 ooooo 8oooo
`-__|__-' 8 8 8 8 8
| 8 o 8 8 o 8 8
------+------ ooooo 8oooooo ooo8ooo ooooo 8

Welcome to GNU CLISP 2.49 (2010-07-07)

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2010

Type :h and hit Enter for context help.

1138375188115359042500720683412325908775988719550104889511926423007926213026253128219807198313873623249367142467945262585730396763370108588287634307935859228536169768076461655325330594895561504497089881415784429431120234472139830431160928494463989909804023426322743148143289534048191921562190548372637402932062444184731784045161351332132440635187845622960237990904758396685102153901340101176982625767
... ...
7414291083508930334684914589548474523630602734787763752642650108051603681222208730507864063922554184820979550914053901623625867638329589057084622018536880047237486402357211985935911518591908559710461028858365500139708030451183599397419167324308926445889501688971003457521313556691540070097267225658102199913459475663435094075824268339011711162419371946058730204417615921460974678178193304750993733448069907261953901184615106218657480204867492382202923258637344388123757033376532121188827136
Bye.

real 0m1.262s
user 0m0.393s
sys 0m0.007s


It surprised me how much slower Common was here. Doesn't help that it clutters the display with all that junk, either.

[ishpeck@slcatedjamu random_scripts]$ time csharp -e "Console.WriteLine(Math.Pow(16384, 16384));"
Infinity

real 0m0.282s
user 0m0.257s
sys 0m0.033s


I don't know what I did wrong here... I mean, besides trusting a statically typed language to know what to do with so huge a number.

[ishpeck@slcatedjamu ~]$ time newlisp -e "(pow 16384 16384)"
inf

real 0m0.003s
user 0m0.000s
sys 0m0.000s


LAWL! Infinity!

[ishpeck@slcatedjamu ~]$ time echo "16384 ^ 16384" | ghci
GHCi, version 7.0.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> 11383751881153590425007206834123259087759887195501048895119264230079262130262531282198071983138736232493671424679452625857303967633701085882876343079358592285361697680764616553253305948955615044970898814157844294311202344721398304311609284944639899098040234263227431481432895340481919215621905483726374029320624441847317840451613513321324406351878456229602379909047583966851021539013401011769826257676957404281402952903327805218427450813174070127356610070681985063233711349485756209832015121729215883326637070712791610842190143317751469403475091772272813853339332108978692241886036861686808927453829277172625828651167987353825746963657963112993510467282600942907196067360720122591171114006676293631519305885416590991437471347216130890697407762356876575347655577253619932893293940498208852789028004916068096301232236018040376353790242084234188565323331807055067323675796435862521814301621476042455089702836121546051790993426228229751145344623392402214667169038572758032803663416789629773460512893937984259228591369691652175545662478112636691284034481402873964948054118464625665800269342156368538355256024510042425488977514395343407596636171538644187065831696424327385893313059342951868342984338532286065419750225452652610457932531801065478094283507991944278536471050177815
......
1800770781409200926729603012036399122612354223297965849193568220166221823827007473973626415778027283203257859436115043467834039248011308008970373580159115358623592785179693929237325906281417327707511367440182904304414404549092837187511274801071123317414291083508930334684914589548474523630602734787763752642650108051603681222208730507864063922554184820979550914053901623625867638329589057084622018536880047237486402357211985935911518591908559710461028858365500139708030451183599397419167324308926445889501688971003457521313556691540070097267225658102199913459475663435094075824268339011711162419371946058730204417615921460974678178193304750993733448069907261953901184615106218657480204867492382202923258637344388123757033376532121188827136
Prelude> Leaving GHCi.

real 0m1.060s
user 0m0.250s
sys 0m0.173s


And for more LOL's!

[ishpeck@slcatedjamu ~]$ time lynx -dump http://www.wolframalpha.com/input/?i=16384%5E16384
#[1]Wolfram|Alpha

* [2]Home
* [3]Examples
* [4]Products
* [5]Blog
* [6]About

*
+ [7]Wolfram Research
+
+ [8]Wolfram Mathematica
+ [9]Wolfram Demonstrations
+ [10]Wolfram MathWorld
+ [11]Wolfram Science
+ [12]WolframTones
+
+ [13]More

Experimental Feature

You have the Wolfram CDF Player installed and can now enable
Wolfram|Alpha Interactive to:
* Interact with your results using sliders and controls
* Rotate and zoom 3D graphics and visualizations
* Manipulate results directly in your browser

[14]Learn more [15]Remind me later

Experimental Feature


Thanks for helping us test Wolfram|Alpha with CDF!
Try these interactive examples:
* Interactively manipulate parameters
____________________ Submit
* Rotate 3D graphics
____________________ Submit
* Read plot values
____________________ Submit
* Animate dynamic processes
____________________ Submit
* Instantly build interactive interfaces
____________________ Submit


Experimental Feature


Thanks for helping us test Wolfram|Alpha with CDF!
Please tell us what you think:[16]Take a survey

Experimental Feature


[17]Learn more [18]Take a survey

[19]Wolfram|Alpha Computational Knowledge EngineEnter what you want to
calculate or know about:

____________________

Submit

[20]Examples[21]Random

To see full output on this page you need to enable JavaScript in your
browser. [22]More info
Computing
Computing...
__________________________________________________________________

Input:

16384^16384
[23]Save as image [24]Copyable plaintext [25]Mathematica form
__________________________________________________________________
__________________________________________________________________

Decimal approximation:

* [26]More digits

[27]1.13837518811535904250072068341232590877598871955010... 10^69049
__________________________________________________________________
__________________________________________________________________

Power of 10 representation:

10^(10^4.839157747319649)
__________________________________________________________________
__________________________________________________________________

Number length:

69050 decimal digits
__________________________________________________________________
__________________________________________________________________

Last few digits:

...1188827136
__________________________________________________________________

[28]Computed by Wolfram MathematicaDownload as: PDFLive Mathematica

... ...

real 0m1.025s
user 0m0.017s
sys 0m0.010s


Of course, this is all very far from scientific but it was amusing to try. I've noticed that clisp tends to out-perform Python on smaller numbers (I assume because Python's cache is helpind it out when there's a lot of repitition). Nobody who actually _tried_ to compute the desired value botched anything up (the way Python is wont to in some arithmetic situations).

I know Haskell tended to be much faster if you subtracted start-up time (which this test does not).

1 comment:

Dave said...

Have you tried comparing to pypy? It's usually about 20x faster than Python for arithmetic like this.