Execution time is varying every time I run the program using tic toc. Is there any way to solve this?

I am running a code. The execution time of it is varying. If it of the order of milli seconds I understand it. But it is varying of the order of decades of milliseconds.
Like sometimes it is showing (in seconds).
0.189348
0.186602
0.110847
0.205987
22.769279
I am using matlab 2021a with all other apps closed.
Each time I am clearing the workspace and closing and opening the desktop app also. I want to know the correct way to find the execution time. How can do it.

8 Commenti

Does your code access data from an external disk? If so the 22 s might be due to spinning up the disk from an idle state.
yes it does take the data from external disk. 22 seconds is ok.
But what about remaining.?
The variability of the timings around 0.2 s is rather typical in my experience. The factor of two shorter is a "bit good", many users would like to catch that one and use it for work. Is it a problem? For better estimates you should use the timeit-function it should give you a better measure of the run-time.
How do you clear the workspace? clear all removes the loaded Matlab function from the memory and reloading an the re-initialization wastes time. Therefore avoid such brute clearing.
@jan. I am finding the execution time for getting those variables. So I have to clear them to find the execution time for finding them.
I am not clearing what I am using to create them. So how do I clear without removing the loaded funcion.
What are you going to do with it if you do get it, anyway? It is going to be what it is and it will, as you've discovered, change the next time again, anyway, either a little or maybe a lot depending upon what is going on with the system (like the disc access) that you have no control over (like context switching by the OS or user keyboard intervention or ...)
It (the execution time) simply is not a single number -- you can approximate an average or even, perhaps, a distribution by collecting a large number of samples, but even that has no guarantee it will be even particularly close to what will be the case when use that data later when conditions have changed (other memory used, other system loadings, etc., etc., ...)
See: doc clear
Use e.g. clear variables or clear with the specific names of variables your want to delete.
clear is rarely useful in Matlab and used too often without a good reason. You do not have to clear a variable to calculate it again.
As @Bjorn Gustavsson suggested, I used timeit function and loaded the data from external disc seperately and passed it to the function.
The time is only varying of the order of milli secconds only.
I think its good result.

Accedi per commentare.

Risposte (1)

Hello,
As per the data provided it seems the tic and toc functions display approximately same values except for times when values exceed the order of 10.
The tic and toc functions returned incorrect results because:
  1. The standard processor driver presented the wrong information to the Windows QueryPerformance interface about which timer to use.
  2. In those cases where the CPU independent APIC clock should have been used, the CPU cycle counter was used instead. The CPU cycle counter can be used only if the processor speed does not vary and if all the cores in the multi-core case appear to be synchronized.
As a solution to this issue, I would like to suggest to:
Install the patch for your hardware starting with the corresponding URL shown below. Reboot your system and rerun the required MATLAB file. It should now pass.
1. Athlon processors
There is one patch that can be applied for both Windows XP and
Windows XP64. Look for a title that begins:
----------------------------------
AMD Athlon 64/FX Processor Driver for Windows XP and
Windows Server 2003 Version (x86 and x64 exe)
----------------------------------
The version and date will change for time. The latest should always work.
2. Opteron processors
There is one patch that can be applied for both Windows XP and
Windows XP64. Look for a title that begins:
----------------------------------
MD Opteron Processor with AMD PowerNow Technology Driver
for Windows XP and Windows Server 2003 Version
(x86 and x64 exe)
----------------------------------
The version and date will change for time. The latest should always work.
The AMD patch installs a new processor driver and, in certain cases, modifies the boot.ini file .

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by