Skip to content
Snippets Groups Projects
Commit 291cd20c authored by Janina Berger's avatar Janina Berger
Browse files

avarage square

parent 4121d322
Branches
Tags
No related merge requests found
...@@ -55,9 +55,7 @@ def plot_all_lang(input_: dict): ...@@ -55,9 +55,7 @@ def plot_all_lang(input_: dict):
plt.text(index, value + 1, formatted_value, ha='center', va='bottom') plt.text(index, value + 1, formatted_value, ha='center', va='bottom')
plt.show() plt.show()
#x = {"C++": 0.098428, "C": 0.097570, "Mojo": 0.0536247719, "Haskell": 0.14860259, "Python": 38.82756280899048}
#resullts Janina: #resullts Janina:
x = {"C++": 0.002895, "C": 0.005625, "Mojo": 0.000434, "Python": 0.07155} x = {"C++": 0.002587, "C": 0.002669, "Mojo": 0.000394, "Python": 0.063196}
plot_all_lang(x) plot_all_lang(x)
No preview for this file type
...@@ -4,22 +4,31 @@ ...@@ -4,22 +4,31 @@
int main() int main()
{ {
int n; int n;
int a; int rounds = 100;
int a=0;
printf("Enter a number: "); printf("Enter a number: ");
scanf("%d", &n); scanf("%d", &n);
a = 0; double time_spent[rounds];
clock_t start = clock(); for(int t =0; t<rounds;t++){
for(int i = 0; i <n; i++){ a=0;
for(int j = 0; j <n; j++){ clock_t start = clock();
a +=1; for(int i = 0; i <n; i++){
for(int j = 0; j <n; j++){
a +=1;
}
} }
clock_t end = clock();
time_spent[t] = ((double) (end - start)) / CLOCKS_PER_SEC;
} }
clock_t end = clock();
double time_spent = ((double) (end - start)) / CLOCKS_PER_SEC;
double result_time = 0;
for(int t =0; t<rounds;t++){
result_time += time_spent[t];
}
result_time /= rounds;
printf("The square of %d is: %d \n" ,n, a); printf("The square of %d is: %d \n" ,n, a);
printf("Time to calculate: %f seconds \n", time_spent); printf("Time to calculate: %f seconds \n", result_time);
return 0; return 0;
} }
\ No newline at end of file
No preview for this file type
...@@ -3,23 +3,33 @@ using namespace std; ...@@ -3,23 +3,33 @@ using namespace std;
int main() { int main() {
int rounds = 100;
int n; int n;
int a; int a;
printf("Enter a number: "); printf("Enter a number: ");
scanf("%d", &n); scanf("%d", &n);
double time_spent[rounds];
a = 0; a = 0;
clock_t start = clock(); for(int t =0; t<rounds;t++){
for(int i = 0; i <n; i++){ a=0;
for(int j = 0; j <n; j++){ clock_t start = clock();
a +=1; for(int i = 0; i <n; i++){
for(int j = 0; j <n; j++){
a +=1;
}
} }
clock_t end = clock();
time_spent[t] = ((double) (end - start)) / CLOCKS_PER_SEC;
} }
clock_t end = clock();
double time_spent = ((double) (end - start)) / CLOCKS_PER_SEC;
double result_time = 0;
for(int t =0; t<rounds;t++){
result_time += time_spent[t];
}
result_time /= rounds;
printf("The square of %d is: %d \n" ,n, a); printf("The square of %d is: %d \n" ,n, a);
printf("Time to calculate: %f seconds \n", time_spent); printf("Time to calculate: %f seconds \n", result_time);
return 0; return 0;
} }
\ No newline at end of file
No preview for this file type
...@@ -4,15 +4,22 @@ fn main()raises: ...@@ -4,15 +4,22 @@ fn main()raises:
let py = Python.import_module("builtins") # to use python 'input' let py = Python.import_module("builtins") # to use python 'input'
let datetime = Python.import_module("datetime") let datetime = Python.import_module("datetime")
let n = py.int(py.input("Enter a number: ")) let n = py.int(py.input("Enter a number: "))
let now = datetime.datetime.now()
var time_diff = now - now #get right type for variable 8messy, since it's a Python type
var a = 0 var a = 0
let start = datetime.datetime.now() for i in range(100):
# calculate square of input value <n> a = 0
for i in range (n): let start = datetime.datetime.now()
for j in range (n): # calculate square of input value <n>
a += 1 for i in range (n):
let end = datetime.datetime.now() for j in range (n):
let time_diff = end - start a += 1
let end = datetime.datetime.now()
time_diff += (end - start) # accumulator for avarage
time_diff = time_diff/100 #get avarage calculation time
print("The square of ",n," is: ",a) print("The square of ",n," is: ",a)
print("Time to calculate: ",time_diff.total_seconds()%60," seconds") print("Time to calculate: ",time_diff.total_seconds()%60," seconds")
...@@ -2,12 +2,16 @@ import datetime ...@@ -2,12 +2,16 @@ import datetime
n = int(input("Enter a number: ")) n = int(input("Enter a number: "))
a = 0 a = 0
start = datetime.datetime.now() now = datetime.datetime.now()
for i in range (n): time_diff = now - now
for j in range (n): for i in range (100):
a += 1 a=0
end = datetime.datetime.now() start = datetime.datetime.now()
time_diff = end - start for i in range (n):
for j in range (n):
a += 1
end = datetime.datetime.now()
time_diff += end - start
time_diff= time_diff/100
print("The square of ",n," is: ",a) print("The square of ",n," is: ",a)
print("Time to calculate: ",time_diff.total_seconds()%60," seconds") print("Time to calculate: ",time_diff.total_seconds()%60," seconds")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment