A really, really strange situation (the same two copies of code on the same computer with the same version of matlab running at very different times)

7 views (last 30 days)
The same two copies of code on the same computer with the same version of matlab running at very different times!
These two pieces of code I guarantee are identical and the parameters are the same (if anything, the comments may be different and the spaces are different,also the name of the main function is different, but I guarantee that the other sub-functions are exactly the same).Their running times are five times different, and I suspect that the images they draw are also different, the strange situation that is driving me crazy.

Accepted Answer

Jan
Jan on 12 Jul 2022
Edited: Jan on 12 Jul 2022
You have two codes. As far as I can see, you are posting one of them. You mention, that comments and spaces can be different. The code is huge - too huge to be understood completely by a reader in the forum. So what kind of help do you expect?
Use the profiler to find the part, which takes more time. If the results are different, you can be sure, that different calculations are performed. This means, that either your guarantee, that both codes are equivalent, is false, or that one of the codes calls other subfunctions e.g. in a /private subfolder or inside the same folder.
The management of codes is not trivial. You need a version history, where you log all changes. Comparing the files reveal all differences. A tool for stripping trailing spaces helps to focus on significant differences. After each change of a function, you have to control if the output is affected.
The i5 12000 has different types of cores. The schedular of Windows 10 cannot identify the difference. So with some bad luck, you are running one code on performance cores and the other one on efficiency cores.
  6 Comments

Sign in to comment.

More Answers (3)

Steven Lord
Steven Lord on 12 Jul 2022
Just a couple comments / ideas.
clear; clc; close all
function []=Ewald_Zero_Seek_1()
clear;clc;close all
Get rid of the second line.
At the time the clear statement executes there are no variables in the workspace, so it's useless. But if you later added an input argument to this function, that input would get thrown in the trash by that line.
Calling clc makes it more difficult to compare the results of two sequentual runs of your code, to see the impact of your changes.
Calling close all is IMO the least annoying part of this, under normal circumstances. Side effects can be good when you expect them to happen, but nothing in the name of your code says that it has the side effect of closing all figure windows.
parfor
parfor uu=1:t_num
What parallel pool(s) are you using to perform the two runs that include this parfor statement?
Are you performing the two runs simultaneously (in which case they're likely contending for worker resources), sequentially (one right after the other), or at different times (not immediately back-to-back)?
What else is running?
On a related note, what else is running on your machine when you perform the two runs? If during one of those runs the machine is basically idle but during the other it's downloading updates from Windows Update, starting up Microsoft Word, or running some other memory and/or CPU intensive process then you're comparing apples and oranges.
Pseudorandom numbers
I did a cursory search for rand in your code but didn't see anything immediately obvious. But I didn't dig through all the code to determine if you were calling something that has non-deterministic behavior. Even though rand is a built-in function and so you can't set a breakpoint in that function, you can set a breakpoint and have MATLAB stop immediately before that function. Do the same for the other pseudorandom number generation functions in MATLAB as well.
dbstop in rand
dbstop in randn
dbstop in randi
dbstop in randperm
After you've tried this and either confirmed or proved that your code doesn't use pseudorandom numbers, clear those breakpoints.
dbclear all
Alternately you could set the state of the random number generator to the same value at the start of each run with rng to try to reduce or eliminate the effect of randomness.
rng default % or
rng(someArbitraryNumber, 'twister')

ma Jack
ma Jack on 13 Jul 2022
Sorry, please give me some time to double check my formulas, I found some parts of my formulas that don't seem to match the actual physical world, also, I unified the expressions for E and they therefore have the same running time, anyway thanks a lot for the discussions, they were very enlightening.
  1 Comment
Steven Lord
Steven Lord on 13 Jul 2022
I am biased, but for code this long with this many subfunctions you might want to split some or all of them into their own files and write unit tests to ensure that they behave the way you expect them to. This will give you some confidence that if you need to modify your code to add a new feature or fix a bug that you are less likely to introduce a new bug in code that used to work.
Don't worry if you don't know how to write test classes or test functions. Even a simple test script can be helpful in locking down behavior. It's a good habit to develop (along with things like commenting your code) if you're going to write more software in the future.

Sign in to comment.


chrisw23
chrisw23 on 27 Jul 2022
Switch Off Intel SpeedStep and Set OS to max Performance...

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!