I am trying to do some Monte Carlo simulation, and as it is with this kind of simulation, it requires a lot of iterations, even for the smallest system. Now I want to do some tweaks with my previous code but it increases the wall time or running time, by 10 fold, which makes a week of calculations to more than two months. I wonder whether I am doing the most efficient way to do the simulation.
Before that, I was using a set of fixed intervals to get the properties of the simulations, but now I want to record a set of random intervals to get the system information as it is the most logical thing to do. However I don't know how to do it.
The code that I was using was basically something like that:
for(long long int it=0; it<numIterations; it)
{
if((numIterations>=10) && (it%1000==0))
{
exportedStates = system.GetStates();
Export2D(exportedStates, outputStatesFile1000, it);
}
}
As you see, before the tweaks made it was going through the simulation and only record the data, every 1000th iterations.
Now I want to do something like this
for(long long int it=0; it<numIterations; it)
{
for(int j = 1; j <= n_graph_points; j){
for (int i = 0; i < n_data_per_graph_points; i){
if (it == initial_position_array[j][i] || it == (initial_position_array[j][i] delta_time_arr[j])) {
exportedStates = system.GetStates();
Export2D(exportedStates, outputStatesFile, it);
}
}
}
}
In this part, the initial position array is just an array with lots of random numbers. The two for loop inside of each other checks every iteration and if the iterations is equal to that random number, it starts recording. I know this is not the best method as it is checking lots of iterations that are not necessary. But, I don't know how can I improve my code. I am a little helpless at this point, so any comment would be appreciated
CodePudding user response:
This does not answer the implied question
[What is the] most efficient way to have [an] if
statement in C (all of them should be equivalent), but
Supposing varying intervals between exports were logical
, how do I code that adequately?
Keep a sane Monte Carlo control, initialise a nextExport
variable to a random value to your liking, and whenever it
equals nextExport
, export and increase nextExport
by the next random interval.
CodePudding user response:
convert your ifs to ternary operator ?: . It is better then branching of if.