티스토리 뷰

언어/Julia

julia vs c

newlibra 2024. 5. 19. 01:40

========================================================================

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

'언어 > Julia' 카테고리의 다른 글

bond pricing with chatGPT vs claude in Adot.  (0) 2025.03.17
machine learning using chatGPT in adot.  (0) 2025.03.17
괜찮은 자료  (0) 2024.05.05
basic  (0) 2024.02.13
ref  (0) 2024.02.13
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함