{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/12859","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/12859/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/12859/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/12859/events","html_url":"https://github.com/JuliaLang/julia/issues/12859","id":103800891,"number":12859,"title":"Performance of sin, cos","user":{"login":"dressel","id":4174250,"avatar_url":"https://avatars.githubusercontent.com/u/4174250?v=3","gravatar_id":"","url":"https://api.github.com/users/dressel","html_url":"https://github.com/dressel","followers_url":"https://api.github.com/users/dressel/followers","following_url":"https://api.github.com/users/dressel/following{/other_user}","gists_url":"https://api.github.com/users/dressel/gists{/gist_id}","starred_url":"https://api.github.com/users/dressel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dressel/subscriptions","organizations_url":"https://api.github.com/users/dressel/orgs","repos_url":"https://api.github.com/users/dressel/repos","events_url":"https://api.github.com/users/dressel/events{/privacy}","received_events_url":"https://api.github.com/users/dressel/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JuliaLang/julia/labels/performance","name":"performance","color":"d7e102"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-08-28T21:43:11Z","updated_at":"2015-08-28T22:09:12Z","closed_at":"2015-08-28T22:09:12Z","body":"I implemented some code in C and Julia, and was surprised about how much slower Julia was. I suspected an inner loop that called `cos`, so I ran repeated calls to `cos` in both C and Julia with the following script:\r\n```\r\n# Using Julia\r\nfunction cos_j ( start_val :: Float64 , end_val :: Float64 )\r\n    step = (1e-6)\r\n    for t = start_val : step : end_val\r\n        for i = 1:200\r\n            cos ( t )\r\n        end\r\n    end\r\nend\r\n\r\n# Using C\r\nfunction cos_c ( start_val :: Float64 , end_val :: Float64 )\r\n    run ( ‘./ct $start_val $end_val‘)\r\nend\r\n\r\n# Force functions to compile first\r\ncos_j (0.0 , 1.0)\r\nrun ( ‘gcc ct.c -o ct')\r\ncos_c (0.0 , 1.0)\r\ntime ()\r\n\r\n# Julia\r\nt1 = time ()\r\ncos_j (0.0 , 1.0)\r\nt2 = time ()\r\nprintln ( \" \\ ncos_j (0 ,1) completes in \" , round ( t2 - t1 ,3) ,\" sec \" )\r\nt1 = time ()\r\ncos_j (1.0 , 2.0)\r\nt2 = time ()\r\nprintln ( \" cos_j (1 ,2) completes in \" , round ( t2 - t1 ,3) ,\" sec \" )\r\n\r\n# C\r\nt1 = time ()\r\ncos_c (0.0 , 1.0)\r\nt2 = time ()\r\nprintln ( \" cos_c (0 ,1) completes in \" , round ( t2 - t1 ,3) ,\" sec \" )\r\nt1 = time ()\r\ncos_c (1.0 , 2.0)\r\nt2 = time ()\r\nprintln ( \" cos_c (1 ,2) completes in \" , round ( t2 - t1 ,3) ,\" sec \" )\r\n```\r\n\r\nThe C code:\r\n```\r\n/∗ ct.c ∗/\r\n#include <stdio.h>\r\n#include <stdlib.h>    // for atof\r\n#include <math.h>    // for cos\r\n\r\nint main ( int argc, char ∗argv[] )\r\n{\r\n    double t ;\r\n    double step = 1e−6;\r\n    double startval = atof ( argv [1] ) ;\r\n    double endval = atof ( argv [2] ) ;\r\n    int i ;\r\n    for ( t = startval ; t < endval ; t += step )\r\n    {\r\n        for ( i = 0 ; i < 200 ; i++)\r\n            cos ( t ) ;\r\n    }\r\n    return 0 ;\r\n}\r\n```\r\nThe output from the Julia script:\r\n```\r\ncos_j (0, 1) completes in 1.267 sec\r\ncos_j (1, 2) completes in 1.917 sec\r\ncos_c (0, 1) completes in 0.388 sec\r\ncos_c (1, 2) completes in 0.384 sec\r\n```\r\nNot only was the Julia code significantly slower, its execution time varied with the time range, whereas the C code's execution time was relatively constant. I'm not sure how the processor computes cosine, but I assume it involves a Taylor expansion--expanding for the time range (0,1) might be easier.\r\n\r\nI assumed that Julia's calls to sin and cos would be the same as C's. Is it possible Julia's call to cos asks for higher precision? Or am I making some mistake in the Julia code (type stability, memory issues, etc.)?\r\n\r\nI also tried using `ccall`, but the results were actually slower than either of these. I didn't include it for brevity, but I can put it up if it helps understand the issue. Any ideas what this could be?"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/135899239","html_url":"https://github.com/JuliaLang/julia/issues/12859#issuecomment-135899239","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/12859","id":135899239,"user":{"login":"yuyichao","id":712232,"avatar_url":"https://avatars.githubusercontent.com/u/712232?v=3","gravatar_id":"","url":"https://api.github.com/users/yuyichao","html_url":"https://github.com/yuyichao","followers_url":"https://api.github.com/users/yuyichao/followers","following_url":"https://api.github.com/users/yuyichao/following{/other_user}","gists_url":"https://api.github.com/users/yuyichao/gists{/gist_id}","starred_url":"https://api.github.com/users/yuyichao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yuyichao/subscriptions","organizations_url":"https://api.github.com/users/yuyichao/orgs","repos_url":"https://api.github.com/users/yuyichao/repos","events_url":"https://api.github.com/users/yuyichao/events{/privacy}","received_events_url":"https://api.github.com/users/yuyichao/received_events","type":"User","site_admin":false},"created_at":"2015-08-28T22:09:12Z","updated_at":"2015-08-28T22:09:12Z","body":"With GCC with default optimization level, the assembly dump actually doesn't call the cos function at all. (and you get a warning saying the `cos(t);` line is doing nothing with `-Wall`).\r\n"}}