{"id":569,"date":"2018-02-08T21:04:52","date_gmt":"2018-02-08T21:04:52","guid":{"rendered":"http:\/\/perfectionatic.org\/?p=569"},"modified":"2018-02-08T21:04:52","modified_gmt":"2018-02-08T21:04:52","slug":"when-julia-is-faster-than-c","status":"publish","type":"post","link":"https:\/\/perfectionatic.org\/?p=569","title":{"rendered":"When Julia is faster than C"},"content":{"rendered":"<p>On e-day, I came across this cool tweet from Fermat&#8217;s library <\/p>\n<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\">\n<p lang=\"en\" dir=\"ltr\">Happy e-day of the century! 2 7 18 <\/p>\n<p>Curious fact about Euler&#39;s Number: Pick a uniformly random number in [0,1] and repeat until the sum of the numbers picked is &gt;1. You&#39;ll on average pick e\u22482.718&#8230; numbers! <a href=\"https:\/\/t.co\/P0wx9hQu79\">pic.twitter.com\/P0wx9hQu79<\/a><\/p>\n<p>&mdash; Fermat&#39;s Library (@fermatslibrary) <a href=\"https:\/\/twitter.com\/fermatslibrary\/status\/961238636418293763?ref_src=twsrc%5Etfw\">February 7, 2018<\/a><\/p><\/blockquote>\n<p><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<p>So I spend a few  minutes coding this into  <code>Julia<\/code><\/p>\n<pre lang=\"julia\">\nfunction euler(n)\n    m=0\n    for i=1:n\n        the_sum=0.0\n        while true\n            m+=1\n            the_sum+=rand()\n            (the_sum>1.0) && break;\n        end\n    end\n    m\/n\nend\n<\/pre>\n<p>Timing this on my machine, I got<\/p>\n<pre lang=\"julia\">\njulia> @time euler(1000000000)\n 15.959913 seconds (5 allocations: 176 bytes)\n2.718219862\n<\/pre>\n<p>Gave a little under 16 seconds.<\/p>\n<p>Tried a <code>c<\/code> implementation<\/p>\n<pre lang=\"c\">\n#include <stdio.h>      \/* printf, NULL *\/\n#include <stdlib.h>     \/* srand, rand *\/\n#include <time.h>       \/* time *\/\n\ndouble r2()\n{\n    return (double)rand() \/ (double)((unsigned)RAND_MAX + 1);\n}\n\ndouble euler(long int n)\n{\n    long int m=0;\n    long int i;\n    for(i=0; i<n; i++){\n        double the_sum=0;\n        while(1) {\n            m++;\n            the_sum+=r2();\n            if(the_sum>1.0) break;\n        }\n    }\n    return (double)m\/(double)n;\n}\n\n\nint main ()\n{\n  printf (\"Euler : %2.5f\\n\", euler(1000000000));\n\n  return 0;\n}\n<\/pre>\n<p>and compiling with either <code>gcc<\/code><\/p>\n<pre lang=\"bash\">\ngcc  -Ofast euler.c\n<\/pre>\n<p>or <code>clang<\/code><\/p>\n<pre lang=\"bash\">\nclang  -Ofast euler.c\n<\/pre>\n<p>gave a timing twice as long<\/p>\n<pre lang=\"bash\">\n$ time .\/a.out \nEuler : 2.71829\n\nreal    0m36.213s\nuser    0m36.238s\nsys 0m0.004s\n<\/pre>\n<p>For the curios, I am using this version of <code>Julia<\/code><\/p>\n<pre lang=\"julia\">\njulia> versioninfo()\nJulia Version 0.6.3-pre.0\nCommit 93168a6 (2017-12-18 07:11 UTC)\nPlatform Info:\n  OS: Linux (x86_64-linux-gnu)\n  CPU: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz\n  WORD_SIZE: 64\n  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)\n  LAPACK: libopenblas64_\n  LIBM: libopenlibm\n  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)\n<\/pre>\n<p>Now one should not put <a href=\"https:\/\/stackoverflow.com\/a\/20007598\/3502221\">too much emphasis on such micro benchmarks<\/a>. However, I found this a very curious examples when a high level language like <code>Julia<\/code> could be twice as fast a <code>c<\/code>. The <code>Julia<\/code> language authors must be doing some amazing mojo.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On e-day, I came across this cool tweet from Fermat&#8217;s library Happy e-day of the century! 2 7 18 Curious fact about Euler&#39;s Number: Pick a uniformly random number in [0,1] and repeat until the sum of the numbers picked is &gt;1. You&#39;ll on average pick e\u22482.718&#8230; numbers! pic.twitter.com\/P0wx9hQu79 &mdash; Fermat&#39;s Library (@fermatslibrary) February 7, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74,73,62,71,1],"tags":[92],"class_list":["post-569","post","type-post","status-publish","format-standard","hentry","category-julia","category-julialang","category-math","category-programming","category-uncategorized","tag-julia"],"_links":{"self":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=569"}],"version-history":[{"count":9,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/569\/revisions"}],"predecessor-version":[{"id":642,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/569\/revisions\/642"}],"wp:attachment":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}