Open alphakilo11 opened 1 year ago
from timeit import default_timer as timer
def nth_prime(n):
"""
This function takes an integer n and returns the nth prime number.
Source: ChatGPT Feb 13 Version
"""
if n <= 0:
return None
primes = [2]
num = 3
counter = len(primes)
while counter < n:
is_prime = True
sqrt_num = int(num ** 0.5) + 1
for p in primes:
if p > sqrt_num:
break
if num % p == 0:
is_prime = False
break
if is_prime:
primes.append(num)
counter += 1
num += 2
return primes[-1]
times = []
results = []
for i in range(10):
start_time = timer()
results.append(nth_prime(50000))
times.append(timer() - start_time)
print(f'{times}\n{results}\nMinimum: {min(times)} s.')
import time
import numpy as np
def estimate_flops():
"""Estimates the FLOPS of the CPU"""
size = 10000
a = np.random.randn(size, size)
b = np.random.randn(size, size)
c = np.zeros((size, size))
start_time = time.time()
for i in range(size):
for j in range(size):
c[i,j] = a[i,j] * b[i,j]
end_time = time.time()
flops = size**3 / (end_time - start_time)
return flops
print(estimate_flops())
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 700
int main() {
double **a, **b, **c;
int i, j, k;
a = (double **) malloc(SIZE * sizeof(double *));
b = (double **) malloc(SIZE * sizeof(double *));
c = (double **) malloc(SIZE * sizeof(double *));
for (i = 0; i < SIZE; i++) {
a[i] = (double *) malloc(SIZE * sizeof(double));
b[i] = (double *) malloc(SIZE * sizeof(double));
c[i] = (double *) malloc(SIZE * sizeof(double));
}
srand(time(NULL));
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
a[i][j] = (double) rand() / RAND_MAX;
b[i][j] = (double) rand() / RAND_MAX;
c[i][j] = 0.0;
}
}
clock_t start_time = clock();
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
clock_t end_time = clock();
double elapsed_time = (double) (end_time - start_time) / CLOCKS_PER_SEC;
double flops = SIZE * SIZE * SIZE / elapsed_time;
printf("FLOPS: %.2f\n", flops);
for (i = 0; i < SIZE; i++) {
free(a[i]);
free(b[i]);
free(c[i]);
}
free(a);
free(b);
free(c);
return 0;
}
I used Python code to compare different online IDEs. Caveat: Might be misleading as it seems that some IDE scale performance. (eg google colab produced 1.3, 0.7 and 1.0 within 2 minutes). 10 repetitions IOT not to exceed the godbolt.org timelimit
Python
estimate_flops();nth_prime();IDE;date 2.04E+10;21201645629.897293;0.5088251719935215;www.onlinegdb.com;20230303 None ;0.31364912099979847;godbolt.org;20230303 None ;1.8811287880016607;replit.com;20230303 2.57E+10;1.0256259109999917;colab.research.google.com;20230303
C
252522463.82;godbolt.org;20230303 702007108.52;replit.com;20230303