julia vs c
========================================================================
julia> function add_numbers(n)
sum = 0
for i in 1:n
sum += i
end
return sum
end
add_numbers (generic function with 1 method)
julia> @time println(add_numbers(1000000))
500000500000
0.031193 seconds (7.15 k allocations: 526.438 KiB, 99.09% compilation time)
julia> @time println(add_numbers(1000000))
500000500000
0.000252 seconds (6 allocations: 176 bytes)
julia> @time println(add_numbers(1000000))
500000500000
0.000165 seconds (6 allocations: 176 bytes)
========================================================================
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
long long add_numbers(int n) {
long long sum = 0;
for(int i=1;i<=n;i++) {
sum += i;
}
return sum;
}
int main() {
struct timeval tv;
double begin, end;
gettimeofday(&tv, NULL);
begin = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
printf("%lld\n", add_numbers(1000000));
gettimeofday(&tv, NULL);
end = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
printf("Execution time %f\n", (end - begin) / 1000);
return (0);
}
======================================================================
$ ./test2
500000500000
Execution time 0.002000