Optimiza el código para tu procesador
By: Date: abril 23, 2020 Categories: Programación

El siguiente script crea un archivo de codigo fuente en C que hace operaciones matematicas y luego lo compila una vez sin ningun tipo de optimizacion, lo ejecuta y lo vuelve a compilar una optimizacion adicional para ejecutarlo, así mide el tiempo que le toma al CPU ejecutar el código, esta optimizacion no se aplica al 100% de los CPU pero en la gran mayoria se podra ver una mejora al ser optimizado.

El articulo y el script son el ejemplo del siguiente video.

Script en el video.

#! /bin/bash

cat > programa.c << EOF

#include <stdio.h>

double
powern (double d, unsigned n)
{
double x = 1.0;
unsigned j;

for (j = 1; j <= n; j++)
x *= d;

return x;
}

int
main (void)
{
double sum = 0.0;
unsigned i;

for (i = 1; i <= 1000000000; i++)
{
sum += powern (i, i % 5);
}

printf (“sum = %g\n”, sum);
return 0;
}
EOF

echo “Compilando y ejecutando sin optimizacion”
gcc -Wall -O0 programa.c -lm -o programa
ls -l programa
time ./programa
rm -f programa
echo “”

echo “Compilando y Optimizando nivel 1”
gcc -Wall -O1 programa.c -lm -o programa
ls -l programa
time ./programa
rm -f programa
echo “”

4 thoughts on “Optimiza el código para tu procesador

  1. Mozilla Firefox 75.0 Linux

    Saludos, Last no se que estoy haciendo mal.
    Apreciaría tu ayuda.
    Le di permisos y hasta lo ejecute como root.
    Al final agrego el comando cat /proc/cpuinfo
    ———————————————————————————–
    ************************************************
    ———————————————————————————–
    root@debian:/home/momon/lastdragon_prueba# ./programa.sh
    “Compilando y ejecutando sin optimizacion”
    programa.c:2:8: error: expected ‘;’ before ‘double’
    include
    ^
    ;
    programa.c:4:1:
    double
    ~~~~~~
    programa.c: In function ‘main’:
    programa.c:27:1: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    printf (“sum = %g\n”, sum);
    ^~~~~~
    programa.c:27:1: warning: incompatible implicit declaration of built-in function ‘printf’
    programa.c:27:1: note: include ‘’ or provide a declaration of ‘printf’
    programa.c:1:1:
    +#include

    programa.c:27:1:
    printf (“sum = %g\n”, sum);
    ^~~~~~
    programa.c:27:9: error: stray ‘\342’ in program
    printf (���sum = %g\n”, sum);
    ^
    programa.c:27:10: error: stray ‘\200’ in program
    printf (���sum = %g\n”, sum);
    ^
    programa.c:27:11: error: stray ‘\234’ in program
    printf (��sum = %g\n”, sum);
    ^
    programa.c:27:18: error: expected expression before ‘%’ token
    printf (“sum = %g\n”, sum);
    ^
    programa.c:27:20: error: stray ‘\’ in program
    printf (“sum = %g\n”, sum);
    ^
    programa.c:27:22: error: stray ‘\342’ in program
    printf (“sum = %g\n���, sum);
    ^
    programa.c:27:23: error: stray ‘\200’ in program
    printf (“sum = %g\n���, sum);
    ^
    programa.c:27:24: error: stray ‘\235’ in program
    printf (“sum = %g\n��, sum);
    ^
    ls: no se puede acceder a ‘programa’: No existe el fichero o el directorio
    ./programa.sh: línea 38: ./programa: No existe el fichero o el directorio

    real 0m0,000s
    user 0m0,001s
    sys 0m0,000s
    “”
    “Compilando y Optimizando nivel 1”
    programa.c:2:8: error: expected ‘;’ before ‘double’
    include
    ^
    ;
    programa.c:4:1:
    double
    ~~~~~~
    programa.c: In function ‘main’:
    programa.c:27:1: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    printf (“sum = %g\n”, sum);
    ^~~~~~
    programa.c:27:1: warning: incompatible implicit declaration of built-in function ‘printf’
    programa.c:27:1: note: include ‘’ or provide a declaration of ‘printf’
    programa.c:1:1:
    +#include

    programa.c:27:1:
    printf (“sum = %g\n”, sum);
    ^~~~~~
    programa.c:27:9: error: stray ‘\342’ in program
    printf (���sum = %g\n”, sum);
    ^
    programa.c:27:10: error: stray ‘\200’ in program
    printf (���sum = %g\n”, sum);
    ^
    programa.c:27:11: error: stray ‘\234’ in program
    printf (��sum = %g\n”, sum);
    ^
    programa.c:27:18: error: expected expression before ‘%’ token
    printf (“sum = %g\n”, sum);
    ^
    programa.c:27:20: error: stray ‘\’ in program
    printf (“sum = %g\n”, sum);
    ^
    programa.c:27:22: error: stray ‘\342’ in program
    printf (“sum = %g\n���, sum);
    ^
    programa.c:27:23: error: stray ‘\200’ in program
    printf (“sum = %g\n���, sum);
    ^
    programa.c:27:24: error: stray ‘\235’ in program
    printf (“sum = %g\n��, sum);
    ^
    ls: no se puede acceder a ‘programa’: No existe el fichero o el directorio
    ./programa.sh: línea 45: ./programa: No existe el fichero o el directorio

    real 0m0,001s
    user 0m0,001s
    sys 0m0,000s
    “”
    root@debian:/home/momon/lastdragon_prueba#

    ———————————————————————————–
    ************************************************
    ———————————————————————————–

    root@debian:/home/momon/lastdragon_prueba# cat /proc/cpuinfo
    processor : 0
    vendor_id : GenuineIntel
    cpu family : 6
    model : 69
    model name : Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz
    stepping : 1
    microcode : 0x25
    cpu MHz : 1063.183
    cache size : 4096 KB
    physical id : 0
    siblings : 4
    core id : 0
    cpu cores : 2
    apicid : 0
    initial apicid : 0
    fpu : yes
    fpu_exception : yes
    cpuid level : 13
    wp : yes
    flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d
    bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
    bogomips : 5187.98
    clflush size : 64
    cache_alignment : 64
    address sizes : 39 bits physical, 48 bits virtual
    power management:

    processor : 1
    vendor_id : GenuineIntel
    cpu family : 6
    model : 69
    model name : Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz
    stepping : 1
    microcode : 0x25
    cpu MHz : 1050.270
    cache size : 4096 KB
    physical id : 0
    siblings : 4
    core id : 0
    cpu cores : 2
    apicid : 1
    initial apicid : 1
    fpu : yes
    fpu_exception : yes
    cpuid level : 13
    wp : yes
    flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d
    bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
    bogomips : 5187.98
    clflush size : 64
    cache_alignment : 64
    address sizes : 39 bits physical, 48 bits virtual
    power management:

    processor : 2
    vendor_id : GenuineIntel
    cpu family : 6
    model : 69
    model name : Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz
    stepping : 1
    microcode : 0x25
    cpu MHz : 1060.188
    cache size : 4096 KB
    physical id : 0
    siblings : 4
    core id : 1
    cpu cores : 2
    apicid : 2
    initial apicid : 2
    fpu : yes
    fpu_exception : yes
    cpuid level : 13
    wp : yes
    flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d
    bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
    bogomips : 5187.98
    clflush size : 64
    cache_alignment : 64
    address sizes : 39 bits physical, 48 bits virtual
    power management:

    processor : 3
    vendor_id : GenuineIntel
    cpu family : 6
    model : 69
    model name : Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz
    stepping : 1
    microcode : 0x25
    cpu MHz : 1067.234
    cache size : 4096 KB
    physical id : 0
    siblings : 4
    core id : 1
    cpu cores : 2
    apicid : 3
    initial apicid : 3
    fpu : yes
    fpu_exception : yes
    cpuid level : 13
    wp : yes
    flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d
    bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
    bogomips : 5187.98
    clflush size : 64
    cache_alignment : 64
    address sizes : 39 bits physical, 48 bits virtual
    power management:

    root@debian:/home/momon/lastdragon_prueba#

  2. Google Chrome 81.0.4044.122 Windows NT

    Para quienes copien el script del web, tengan en cuenta que muchas veces el navegador cambia las comillas dobles y esto les daría un error de compilación.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *