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

Skip to content

Commit f1f1154

Browse files
committed
Finished Version until 3.3
1 parent 297e3fe commit f1f1154

File tree

69 files changed

+746
-515
lines changed
  • javascript
  • python
  • version-2
  • prueba-2
  • prueba-3/python
  • Some content is hidden

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

    69 files changed

    +746
    -515
    lines changed

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

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -5,7 +5,7 @@
    55
    import numpy as np
    66
    from pyscript import display
    77

    8-
    seed = int(time.time() * 1000) % 2**32
    8+
    seed = int(time.perf_counter() * 1000) % 2**32
    99
    rng = np.random.default_rng(seed)
    1010

    1111

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

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -5,7 +5,7 @@
    55
    import numpy as np
    66
    import gc
    77

    8-
    seed = int(time.time() * 1000) % 2**32
    8+
    seed = int(time.perf_counter() * 1000) % 2**32
    99
    rng = np.random.default_rng(seed)
    1010

    1111

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

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -15,11 +15,12 @@ function sieveOfEratosthenes(n) {
    1515

    1616
    function getMemoryUsageJS() {
    1717
    if (performance.memory) {
    18-
    return performance.memory.usedJSHeapSize / (1024 * 1024);
    18+
    return Math.abs(performance.memory.usedJSHeapSize / (1024 * 1024), 0);
    1919
    }
    2020
    return -1;
    2121
    }
    2222

    23+
    2324
    function benchmarkPrimesJS(repetitions, n) {
    2425
    let totalTime = 0;
    2526
    let totalMemory = 0;

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

    Lines changed: 7 additions & 7 deletions
    Original file line numberDiff line numberDiff line change
    @@ -7,12 +7,12 @@ const cpu = osu.cpu;
    77

    88
    app.use(cors());
    99

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

    1414
    async function getCpuUsage() {
    15-
    return await cpu.usage();
    15+
    return Math.max(await cpu.usage(), 0);
    1616
    }
    1717

    1818
    function sieveOfEratosthenes(n) {
    @@ -38,15 +38,15 @@ async function benchmarkPrimesJS(repetitions, n) {
    3838
    const startTotal = performance.now();
    3939

    4040
    for (let i = 0; i < repetitions; i++) {
    41-
    const startMemory = getMemoryUsage();
    41+
    const startMemory = getMemoryUsageJS();
    4242
    const start = performance.now();
    4343
    const cpuBefore = await getCpuUsage();
    4444

    4545
    sieveOfEratosthenes(n);
    4646

    4747
    const cpuAfter = await getCpuUsage();
    4848
    const end = performance.now();
    49-
    const endMemory = getMemoryUsage();
    49+
    const endMemory = getMemoryUsageJS();
    5050

    5151
    totalTime += (end - start);
    5252
    totalMemory += (endMemory - startMemory);
    @@ -55,8 +55,8 @@ async function benchmarkPrimesJS(repetitions, n) {
    5555

    5656
    const totalExecTime = (performance.now() - startTotal).toFixed(2);
    5757
    const avgTime = (totalTime / repetitions).toFixed(2);
    58-
    const avgMemory = (totalMemory / repetitions).toFixed(2);
    59-
    const avgCPU = (totalCPU / repetitions).toFixed(2);
    58+
    const avgMemory = Math.abs((totalMemory / repetitions)).toFixed(2);
    59+
    const avgCPU = Math.abs((totalCPU / repetitions)).toFixed(2);
    6060

    6161
    return {
    6262
    totalExecutionTime: `Total ET (50x): ${totalExecTime} ms`,

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

    Lines changed: 4 additions & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -17,16 +17,16 @@ def get_memory_usage():
    1717

    1818

    1919
    def benchmark_primes_py(repetitions, n):
    20+
    gc.collect()
    2021
    tracemalloc.start()
    2122
    start_total_time = time.perf_counter()
    22-
    gc.collect()
    2323

    2424
    total_time = 0
    2525
    total_memory = 0
    2626
    for _ in range(repetitions):
    27+
    gc.collect()
    2728
    tracemalloc.start()
    2829
    start = time.perf_counter()
    29-
    gc.collect()
    3030

    3131
    primes = sieve_of_eratosthenes_np(n)
    3232

    @@ -38,8 +38,8 @@ def benchmark_primes_py(repetitions, n):
    3838
    total_memory += memory_usage
    3939

    4040
    total_exec_time = (time.perf_counter() - start_total_time) * 1000
    41-
    avg_time = total_time / repetitions
    42-
    avg_memory = total_memory / repetitions
    41+
    avg_time = round(total_time / repetitions, 2)
    42+
    avg_memory = round(total_memory / repetitions, 2)
    4343

    4444
    tracemalloc.stop()
    4545

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

    Lines changed: 16 additions & 8 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
    @@ -11,38 +12,45 @@ def sieve_of_eratosthenes(n):
    1112

    1213
    def get_memory_usage():
    1314
    process = psutil.Process(os.getpid())
    14-
    return round(process.memory_info().rss / (1024 * 1024), 2)
    15+
    return max(process.memory_info().rss / (1024 * 1024), 0)
    1516

    1617

    17-
    def get_cpu_usage():
    18-
    return psutil.cpu_percent()
    18+
    def get_cpu_usage(process):
    19+
    return process.cpu_percent(interval=None)
    1920

    2021

    2122
    def benchmark_primes_py(repetitions, n):
    23+
    gc.collect()
    24+
    process = psutil.Process(os.getpid())
    25+
    process.cpu_percent(interval=None)
    26+
    2227
    total_time = 0
    2328
    total_memory = 0
    2429
    total_cpu = 0
    2530

    2631
    start_total = time.perf_counter()
    2732

    2833
    for _ in range(repetitions):
    34+
    gc.collect()
    2935
    start_memory = get_memory_usage()
    3036
    start = time.perf_counter()
    3137

    32-
    cpu_before = get_cpu_usage()
    38+
    cpu_before = get_cpu_usage(process)
    3339
    primes = sieve_of_eratosthenes(n)
    40+
    cpu_after = get_cpu_usage(process)
    3441

    42+
    end = time.perf_counter()
    3543
    memory_usage = get_memory_usage() - start_memory
    36-
    cpu_after = get_cpu_usage() - cpu_before
    44+
    cpu_usage = cpu_after - cpu_before
    3745

    38-
    total_time += (time.perf_counter() - start) * 1000
    46+
    total_time += (end - start) * 1000
    3947
    total_memory += memory_usage
    40-
    total_cpu += cpu_after
    48+
    total_cpu += cpu_usage
    4149

    4250
    total_exec_time = round((time.perf_counter() - start_total) * 1000, 2)
    4351
    avg_time = round(total_time / repetitions, 2)
    4452
    avg_memory = round(total_memory / repetitions, 2)
    45-
    avg_cpu = round(total_cpu / repetitions, 2)
    53+
    avg_cpu = abs(round(total_cpu / repetitions, 2))
    4654

    4755
    return {
    4856
    'total_time': f"Total ET (1000x): {total_exec_time} ms",

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

    Lines changed: 3 additions & 3 deletions
    Original file line numberDiff line numberDiff line change
    @@ -24,10 +24,9 @@ function n_digits_pi(repetitions, digits) {
    2424
    const piValue = calculatePi(digits);
    2525

    2626
    const end = performance.now();
    27-
    const memoryUsage = getMemoryUsageJS();
    2827

    2928
    totalTime += (end - start);
    30-
    totalMemory += memoryUsage;
    29+
    totalMemory += getMemoryUsageJS();
    3130
    }
    3231

    3332
    const endTotal = performance.now();
    @@ -45,11 +44,12 @@ function n_digits_pi(repetitions, digits) {
    4544

    4645
    function getMemoryUsageJS() {
    4746
    if (performance.memory) {
    48-
    return performance.memory.usedJSHeapSize / (1024 * 1024);
    47+
    return Math.max(performance.memory.usedJSHeapSize / (1024 * 1024), 0);
    4948
    }
    5049
    return -1;
    5150
    }
    5251

    52+
    5353
    function displayResults(results) {
    5454
    const output = document.getElementById("javascript-output");
    5555

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

    Lines changed: 18 additions & 16 deletions
    Original file line numberDiff line numberDiff line change
    @@ -21,45 +21,47 @@ function calculatePi(digits) {
    2121
    return pi;
    2222
    }
    2323

    24-
    function getMemoryUsage() {
    25-
    return process.memoryUsage().heapUsed;
    24+
    function getMemoryUsageJS() {
    25+
    return Math.max(process.memoryUsage().heapUsed / (1024 * 1024), 0);
    2626
    }
    2727

    2828
    async function getCpuUsage() {
    29-
    return await cpu.usage();
    29+
    return Math.max(await cpu.usage(), 0);
    3030
    }
    3131

    3232
    async function computePi(digits, repetitions) {
    3333
    let totalTime = 0;
    3434
    let totalMemory = 0;
    35-
    let totalCpu = 0;
    35+
    let totalCPU = 0;
    3636

    3737
    let startReal = performance.now();
    3838

    3939
    for (let i = 0; i < repetitions; i++) {
    40-
    const startMemory = process.memoryUsage().heapUsed;
    40+
    const startMemory = getMemoryUsageJS();
    41+
    const start = performance.now();
    42+
    const cpuBefore = await getCpuUsage();
    4143

    42-
    let start = performance.now();
    4344
    let piValue = calculatePi(digits);
    4445

    45-
    let executionTime = performance.now() - start;
    46-
    let memoryUsage = (getMemoryUsage - startMemory) / (1024 * 1024);
    46+
    const cpuAfter = await getCpuUsage();
    47+
    const end = performance.now();
    48+
    const endMemory = getMemoryUsageJS();
    4749

    48-
    totalTime += executionTime;
    49-
    totalMemory += memoryUsage;
    50+
    totalTime += (end - start);
    51+
    totalMemory += (endMemory - startMemory);
    52+
    totalCPU += (cpuAfter - cpuBefore);
    5053
    }
    5154

    52-
    let totalExecTime = ((performance.now() - startReal) / 1000).toFixed(2);
    53-
    totalCpu = await getCpuUsage();
    54-
    55-
    let avgTime = (totalTime / repetitions).toFixed(2);
    56-
    let avgMemory = (totalMemory / repetitions).toFixed(2);
    55+
    const totalExecTime = (performance.now() - startTotal).toFixed(2);
    56+
    const avgTime = (totalTime / repetitions).toFixed(2);
    57+
    const avgMemory = (totalMemory / repetitions).toFixed(2);
    58+
    const avgCPU = (totalCPU / repetitions).toFixed(2);
    5759

    5860
    return {
    5961
    totalExecutionTime: `Total ET (1000x): ${totalExecTime} ms`,
    6062
    avgExecutionTime: `ET (avg, 1000x): ${avgTime} ms`,
    6163
    avgMemoryUsage: `RAM (avg, 1000x): ${avgMemory} MB`,
    62-
    avgCPUUsage: `CPU (avg, 1000x): ${totalCpu} %`
    64+
    avgCPUUsage: `CPU (avg, 1000x): ${avgCPU} %`
    6365
    };
    6466
    }
    6567

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

    Lines changed: 4 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -22,16 +22,18 @@ def get_memory_usage():
    2222

    2323

    2424
    def n_digits_pi(repetitions, digits):
    25+
    gc.collect()
    26+
    tracemalloc.start()
    2527
    total_time = 0
    2628
    total_memory = 0
    2729

    2830
    start_total = time.perf_counter()
    2931

    3032
    for _ in range(repetitions):
    31-
    tracemalloc.start()
    3233
    gc.collect()
    33-
    34+
    tracemalloc.start()
    3435
    start = time.perf_counter()
    36+
    3537
    pi_value = calculate_pi(digits)
    3638

    3739
    end = time.perf_counter()

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

    Lines changed: 17 additions & 10 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1,7 +1,7 @@
    11
    import json
    2+
    import os
    23
    import time
    34
    import psutil
    4-
    import tracemalloc
    55
    import gc
    66

    77

    @@ -18,34 +18,41 @@ def calculate_pi(digits):
    1818

    1919

    2020
    def get_memory_usage():
    21-
    return tracemalloc.get_traced_memory()[1] / (1024 * 1024)
    21+
    process = psutil.Process(os.getpid())
    22+
    return max(process.memory_info().rss / (1024 * 1024), 0)
    2223

    2324

    24-
    def get_cpu_usage():
    25-
    return psutil.cpu_percent()
    25+
    def get_cpu_usage(process):
    26+
    return process.cpu_percent(interval=None)
    2627

    2728

    2829
    def n_digits_pi(repetitions, digits):
    30+
    gc.collect()
    31+
    process = psutil.Process(os.getpid())
    32+
    process.cpu_percent(interval=None)
    33+
    2934
    total_time = 0
    3035
    total_memory = 0
    3136
    total_cpu = 0
    3237

    3338
    start_total = time.perf_counter()
    3439

    3540
    for _ in range(repetitions):
    36-
    tracemalloc.start()
    37-
    start = time.perf_counter()
    3841
    gc.collect()
    42+
    start_memory = get_memory_usage()
    43+
    start = time.perf_counter()
    44+
    45+
    cpu_before = get_cpu_usage(process)
    3946
    pi_value = calculate_pi(digits)
    40-
    cpu_after = get_cpu_usage()
    47+
    cpu_after = get_cpu_usage(process)
    4148

    4249
    end = time.perf_counter()
    43-
    memory_usage = get_memory_usage()
    44-
    tracemalloc.stop()
    50+
    memory_usage = get_memory_usage() - start_memory
    51+
    cpu_usage = cpu_after - cpu_before
    4552

    4653
    total_time += (end - start) * 1000
    4754
    total_memory += memory_usage
    48-
    total_cpu += cpu_after
    55+
    total_cpu += cpu_usage
    4956

    5057
    total_exec_time = round((time.perf_counter() - start_total) * 1000, 2)
    5158
    avg_time = round(total_time / repetitions, 2)

    0 commit comments

    Comments
     (0)
    0