Publi

Cronometrar código (matlab)

Matlab® es un lenguaje muy utilizado en ingeniería, ya que permite hacer de forma más o menos sencilla cálculos matemáticos muy complejos, y a veces necesitamos procesar gran cantidad de datos y llega a ser desesperante ver cómo tarda sin tener tan siquiera una ligera idea de cuánto queda.

El principio del código que voy a postear se basa en dos funciones: tic y toc. La primera de ellas marca el inicio de la cuenta de tiempo, y la segunda el final, indicándonos los segundos transcurridos entre tic y toc. Podemos por ejemplo hacer esto:

1
2
3
tic
% Tarea que tarda mucho
toc

Y nos devolverá los segundos en total que ha tardado.

Pero vamos a ir algo más allá. Imaginemos que tenemos una tarea iterativa de N iteraciones, queremos que cada N/20 iteraciones nos diga más o menos lo que queda (en cada iteración sería mucho, porque estaría todo el rato diciendo el tiempo, y además gastando tiempo en ello; por lo que tendríamos que esperar mucho más de lo necesario).

Éste es el código:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
N=2000;

aviso=100;

tic % Inicia el contador de tiempo
for n=0:N

    % Calculamos el progreso total y los tiempos estimado y restante
    if (mod(n, aviso)==0)
        progreso=n/N;       % Progreso (Entre 0 y 1)
        transcurrido=toc;   % Tiempo transcurrido
        estimado=transcurrido/progreso; % Estimamos el tiempo total
        queda = estimado-transcurrido;  % Tiempo restante
        % Lo mostramos todo en pantalla
        disp(sprintf('Progreso %2.2f%%. Tiempo Transcurrido: %ds. Tiempo estimado: %ds. Tiempo restante %ds.', progreso*100, round(transcurrido), round(estimado), round(queda)));    
    end
   
    % Esto se supone que es una tarea que tarda mucho tiempo
    pause(0.01);
end

disp('Completado!');

También podría interesarte....

There are 6 comments left Ir a comentario

  1. Pingback: Bitacoras.com /

  2. miguek /
    Usando Google Chrome Google Chrome 56.0.2924.87 en Windows Windows 8

    GENIAL! muchísimas gracias, justo lo que necesitaba
    crack

    1. Gaspar Fernández /
      Usando Mozilla Firefox Mozilla Firefox 52.0 en Ubuntu Linux Ubuntu Linux

      Gracias a ti miguek, por pasarte por aquí 🙂

  3. Fool Me Once Mark Wright Black Coat /
    Usando Google Chrome Google Chrome 120.0.0.0 en Windows Windows NT

    Thank you for posting such a great article! I found your website perfect for my needs. It contains wonderful and helpful posts. Keep up the good work!. Thank you for this wonderful Article!

  4. IT assignment help online /
    Usando Google Chrome Google Chrome 85.0.4183.83 en Windows Windows NT

    We can execute a progress-tracking system to maximize the procedure, using a counter variable that increments with each iteration. After that, at periods of N/20 patterns, we can determine and present the staying patterns. In this manner, we attain progression updates without extreme time usage. In addition, including IT assignment help online a standing upgrade feature with ideal regularity ensures a reliable source application is coupled with prompt comments. This strategy stabilizes the demand for progression tracking without endangering efficiency. For help maximizing such formulas, look for support from professionals in IT project aid online.

  5. CIPD Help /
    Usando Google Chrome Google Chrome 109.0.0.0 en Windows Windows NT

    Professional Behaviours and Valuing People (5CO03) is a course that teaches important skills for interacting effectively in the workplace. It covers topics like communication, teamwork, and respecting others. By taking this course, students learn how to behave professionally and value the contributions of their colleagues.
    CIPD Assignment Help for 5CO03 can benefit students by providing guidance on how to apply professional behaviors and value people effectively in assignments. It offers support in understanding course concepts, improving writing skills, and achieving better grades. With this help, students can excel in demonstrating their understanding of professional behaviors and valuing people in their coursework.

Leave a Reply to Gaspar Fernández Cancle Reply