8000 Finished Version until 1.2.1 · JayAyAre/PyScript-vs-JavaScript@297e3fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 297e3fe

Browse files
committed
Finished Version until 1.2.1
1 parent e52dee7 commit 297e3fe

File tree

93 files changed

+1791
-1635
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1791
-1635
lines changed

Astro-TFG/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Inside of your Astro project, you'll see the following folders and files:
1919
```text
2020
/
2121
├── public/
22-
│ └── favicon.svg
2322
├── src/
2423
│ ├── layouts/
2524
│ │ └── Layout.astro

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-1/version-1/javascript/main.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ function multiplyMatrices(size) {
3838

3939
function getMemoryUsageJS() {
4040
if (performance.memory) {
41-
return performance.memory.usedJSHeapSize / (1024 * 1024);
41+
return Math.max(performance.memory.usedJSHeapSize / (1024 * 1024), 0);
4242
}
4343
return -1;
4444
}
4545

46-
4746
function displayResults(results) {
4847
const output = document.getElementById("javascript-output");
4948

@@ -55,13 +54,13 @@ function displayResults(results) {
5554
if (results.memory_usage !== -1) {
5655
memoryDiv.textContent = `RAM: ${results.memory_usage.toFixed(2)} MB`;
5756
} else {
58-
memoryDiv.textContent = `RAM unsupported measurement in this browser.`;
57+
memoryDiv.textContent = `RAM measurement unsupported in this browser.`;
5958
}
6059
output.appendChild(memoryDiv);
6160
}
6261

6362
window.runJsBenchmark = function () {
64-
clearCell("javascript-output");
63+
window.clearCell("javascript-output");
6564
window.showExecutionLoader();
6665

6766
requestAnimationFrame(() => {
@@ -71,5 +70,4 @@ window.runJsBenchmark = function () {
7170
window.hideExecutionLoader();
7271
}, 0);
7372
});
74-
75-
}
73+
};

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-1/version-1/javascript/server.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ function createMatrix(size) {
1414
return matrix;
1515
}
1616

17-
function getMemoryUsage() {
18-
return process.memoryUsage().heapUsed / (1024 * 1024);
17+
18+
function getMemoryUsageJS() {
19+
return Math.max(process.memoryUsage().heapUsed / (1024 * 1024), 0);
1920
}
2021

2122
async function getCpuUsage() {
22-
return await cpu.usage();
23+
return Math.max(await cpu.usage(), 0);
2324
}
2425

2526
async function multiplyMatrices(size) {
26-
const startMemory = getMemoryUsage();
2727

2828
let A = createMatrix(size);
2929
let B = createMatrix(size);
@@ -42,7 +42,7 @@ async function multiplyMatrices(size) {
4242
}
4343

4444
const executionTime = +(performance.now() - startTime).toFixed(2);
45-
const memoryUsage = +((getMemoryUsage() - startMemory)).toFixed(2);
45+
const memoryUsage = +getMemoryUsageJS().toFixed(2);
4646
const endCpu = +(await getCpuUsage()).toFixed(2);
4747

4848
return {
@@ -59,4 +59,4 @@ async function multiplyMatrices(size) {
5959
data: result
6060
};
6161
console.log(JSON.stringify(results));
62-
})();
62+
})();

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-1/version-1/python/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def get_memory_usage():
1515

1616

1717
def multiply_matrices(size):
18+
gc.collect()
1819
tracemalloc.start()
1920

2021
A = create_matrix(size)
@@ -23,7 +24,6 @@ def multiply_matrices(size):
2324
C = [[0] * size for _ in range(size)]
2425

2526
97AE start = time.perf_counter()
26-
gc.collect()
2727

2828
for i in range(size):
2929
for j in range(size):
@@ -33,7 +33,7 @@ def multiply_matrices(size):
3333
C[i][j] = _sum
3434

3535
execution_time = (time.perf_counter() - start) * 1000
36-
memory_usage = get_memory_usage()
36+
memory_usage = abs(get_memory_usage())
3737
tracemalloc.stop()
3838

3939
results = {

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-1/version-1/python/server.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ def get_memory_usage():
1515
return round(process.memory_info().rss / (1024 * 1024), 2)
1616

1717

18-
def get_cpu_usage():
19-
return psutil.cpu_percent()
20-
21-
2218
def multiply_matrices(size):
2319
gc.collect()
2420
start_memory = get_memory_usage()
25-
start_cpu = get_cpu_usage()
21+
process = psutil.Process(os.getpid())
22+
process.cpu_percent(interval=None)
2623

2724
A = create_matrix(size)
2825
B = create_matrix(size)
@@ -40,12 +37,9 @@ def multiply_matrices(size):
4037

4138
end_time = time.perf_counter()
4239

43-
end_memory = get_memory_usage()
44-
end_cpu = get_cpu_usage()
45-
4640
execution_time = round((end_time - start_time) * 1000, 2)
47-
memory_used = round(end_memory - start_memory, 2)
48-
cpu_usage = round(end_cpu - start_cpu, 2)
41+
memory_used = abs(round(get_memory_usage() - start_memory, 2))
42+
cpu_usage = abs(process.cpu_percent(interval=1))
4943

5044
return {
5145
'time': f"ET: {execution_time} ms",

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-1/version-2/javascript/main.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
async function multiplyMatrices(size) {
2-
let A = tf.randomNormal([size, size]);
3-
let B = tf.randomNormal([size, size]);
2+
let A = tf.randomUniform([size, size], 0.0, 1.0);
3+
let B = tf.randomUniform([size, size], 0.0, 1.0);
44

5-
let start = performance.now();
5+
const start = performance.now();
66

77
let C = tf.matMul(A, B);
88

@@ -19,11 +19,12 @@ async function multiplyMatrices(size) {
1919

2020
function getMemoryUsageJS() {
2121
if (performance.memory) {
22-
return performance.memory.usedJSHeapSize / (1024 * 1024);
22+
return Math.max(performance.memory.usedJSHeapSize / (1024 * 1024), 0);
2323
}
2424
return -1;
2525
}
2626

27+
2728
function displayResults(results) {
2829
const output = document.getElementById("javascript-output");
2930

@@ -40,7 +41,7 @@ function displayResults(results) {
4041
if (results.memory_usage !== -1) {
4142
memoryDiv.textContent = `RAM: ${results.memory_usage.toFixed(2)} MB`;
4243
} else {
43-
memoryDiv.textContent = `RAM unsupported measurement in this browser.`;
44+
memoryDiv.textContent = `RAM measurement unsupported in this browser.`;
4445
}
4546
output.appendChild(memoryDiv);
4647
output.appendChild(document.createElement("br"));

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-1/version-2/javascript/server.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,24 @@ const app = express();
88

99
app.use(cors());
1010

11-
function getMemoryUsage() {
12-
return process.memoryUsage().heapUsed / (1024 * 1024);
11+
function getMemoryUsageJS() {
12+
return Math.max(process.memoryUsage().heapUsed / (1024 * 1024), 0);
1313
}
1414

1515
async function getCpuUsage() {
16-
return await cpu.usage();
16+
return Math.max(await cpu.usage(), 0);
1717
}
1818

19-
2019
async function multiplyMatrices(size) {
21-
const startMemory = getMemoryUsage();
22-
23-
let A = tf.randomNormal([size, size]);
24-
let B = tf.randomNormal([size, size]);
20+
let A = tf.randomUniform([size, size], 0.0, 1.0);
21+
let B = tf.randomUniform([size, size], 0.0, 1.0);
2522

2623
const startTime = performance.now();
2724

2825
let C = tf.matMul(A, B);
2926

3027
const executionTime = +(performance.now() - startTime).toFixed(2);
31-
const memoryUsage = +((getMemoryUsage() - startMemory)).toFixed(2);
28+
const memoryUsage = +getMemoryUsageJS().toFixed(2);
3229
const endCpu = +(await getCpuUsage()).toFixed(2);
3330

3431
return {
@@ -38,6 +35,7 @@ async function multiplyMatrices(size) {
3835
memory_usage: `RAM: ${memoryUsage} MB`
3936
};
4037
}
38+
4139
(async () => {
4240
let result = {
4341
matrices500: await multiplyMatrices(500),

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-1/version-2/python/main.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ def get_memory_usage():
1818

1919

2020
def multiply_matrices(size):
21+
gc.collect()
2122
tracemalloc.start()
2223

2324
A = create_matrix(size)
2425
B = create_matrix(size)
2526

2627
start = time.perf_counter()
27-
gc.collect()
2828

2929
C = np.dot(A, B)
3030

31-
end = time.perf_counter()
32-
execution_time = (end - start) * 1000
33-
34-
memory_usage = get_memory_usage()
31+
execution_time = (time.perf_counter() - start) * 1000
32+
memory_usage = abs(get_memory_usage())
3533
tracemalloc.stop()
3634

3735
results = {

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-1/version-2/python/server.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import psutil
44
import os
55
import numpy as np
6+
import gc
67

78
seed = int(time.time() * 1000) % 2**32
89
rng = np.random.default_rng(seed)
@@ -14,32 +15,31 @@ def create_matrix(size):
1415

1516
def get_memory_usage():
1617
process = psutil.Process(os.getpid())
17-
return round(process.memory_info().rss / (1024 * 1024), 2)
18-
19-
20-
def get_cpu_usage():
21-
return psutil.cpu_percent()
18+
return max(process.memory_info().rss / (1024 * 1024), 0)
2219

2320

2421
def multiply_matrices(size):
22+
gc.collect()
2523
start_memory = get_memory_usage()
26-
start_cpu = get_cpu_usage()
24+
process = psutil.Process(os.getpid())
25+
process.cpu_percent(interval=None)
2726

2827
A = create_matrix(size)
2928
B = create_matrix(size)
3029

3130
start_time = time.perf_counter()
3231
C = np.dot(A, B)
32+
end_time = time.perf_counter()
3333

34-
execution_time = round((time.perf_counter() - start_time) * 1000, 2)
35-
memory_used = round(get_memory_usage() - start_memory, 2)
36-
cpu_usage = round(get_cpu_usage() - start_cpu, 2)
34+
execution_time = round((end_time - start_time) * 1000, 2)
35+
memory_used = abs(get_memory_usage() - start_memory)
36+
cpu_usage = abs(process.cpu_percent(interval=1))
3737

3838
return {
3939
'size': f"{size}x{size}",
40-
'time': f"ET: {execution_time} ms",
41-
'cpu_usage': f"CPU: {cpu_usage} %",
42-
'memory_usage': f"RAM: {memory_used} MB"
40+
'time': f"ET: {execution_time:.2f} ms",
41+
'cpu_usage': f"CPU: {cpu_usage:.2f} %",
42+
'memory_usage': f"RAM: {memory_used:.2f} MB"
4343
}
4444

4545
10000

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-2/version-1/javascript/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function primes_to_n(size) {
4343

4444
function getMemoryUsageJS() {
4545
if (performance.memory) {
46-
return performance.memory.usedJSHeapSize / (1024 * 1024);
46+
return Math.max(performance.memory.usedJSHeapSize / (1024 * 1024), 0);
4747
}
4848
return -1;
4949
}

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-2/version-1/javascript/server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import osu from 'node-os-utils';
33
const cpu = osu.cpu;
44

55

6-
function getMemoryUsage() {
7-
return process.memoryUsage().heapUsed / (1024 * 1024);
6+
function getMemoryUsageJS() {
7+
return Math.max(process.memoryUsage().heapUsed / (1024 * 1024), 0);
88
}
99

1010
async function getCpuUsage() {
11-
return await cpu.usage();
11+
return Math.max(await cpu.usage(), 0);
1212
}
1313

1414
function is_prime(size) {
@@ -46,7 +46,7 @@ async function primes_to_n(size) {
4646
}
4747

4848
const executionTime = +(performance.now() - startTime).toFixed(2);
49-
const memoryUsage = +((getMemoryUsage() - startMemory)).toFixed(2);
49+
const memoryUsage = +getMemoryUsageJS().toFixed(2);
5050
const endCpu = +(await getCpuUsage()).toFixed(2);
5151

5252
return {

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-2/version-1/python/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ def get_memory_usage():
2424

2525

2626
def primes_to_n(n):
27+
gc.collect()
2728
tracemalloc.start()
2829

2930
primes = []
3031

3132
start = time.perf_counter()
32-
gc.collect()
3333

3434
if n > 2:
3535
primes.append(2)
@@ -39,7 +39,7 @@ def primes_to_n(n):
3939
primes.append(i)
4040

4141
execution_time = (time.perf_counter() - start) * 1000
42-
memory_usage = get_memory_usage()
42+
memory_usage = abs(get_memory_usage())
4343
tracemalloc.stop()
4444

4545
results = {

Astro-TFG/public/scripts/1-calculos-matematicos/prueba-2/version-1/python/server.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import json
23
import time
34
import psutil
@@ -20,17 +21,15 @@ def is_prime(n):
2021

2122
def get_memory_usage():
2223
process = psutil.Process(os.getpid())
23-
return round(process.memory_info().rss / (1024 * 1024), 2)
24-
25-
26-
def get_cpu_usage():
27-
return psutil.cpu_percent()
24+
return max(process.memory_info().rss / (1024 * 1024), 0)
2825

2926

3027
def primes_to_n(n):
31-
start_memory = get_memory_usage()
32-
start_cpu = get_cpu_usage()
28+
gc.collect()
3329
start_time = time.perf_counter()
30+
start_memory = get_memory_usage()
31+
process = psutil.Process(os.getpid())
32+
process.cpu_percent(interval=None)
3433

3534
primes = []
3635

@@ -42,8 +41,8 @@ def primes_to_n(n):
4241
primes.append(i)
4342

4443
execution_time = round((time.perf_counter() - start_time) * 1000, 2)
45-
memory_used = round(get_memory_usage() - start_memory, 2)
46-
cpu_usage = round(get_cpu_usage() - start_cpu, 2)
44+
memory_used = round(abs(get_memory_usage() - start_memory), 2)
45+
cpu_usage = abs(process.cpu_percent(interval=1))
4746

4847
return {
4948
'time': f"ET: {execution_time} ms",

0 commit comments

Comments
 (0)
0