-1

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

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 4
    Have you profiled your code to verify this is actually where you're spending most of the time? And are you building with optimizations turned on? – Stephen Newell Oct 01 '21 at 04:41
  • 1
    *"Now I want to do something like this"* -- you have defined your intended functionality via code (rather than via requirements). This means that your code is undeniably correct and cannot be improved, since that code is your goal by definition. Better would be to describe your desired functionality in words, relegating the code from "definition" to "example". – JaMiT Oct 01 '21 at 05:31
  • @JaMiT Correct doesn't mean optimal. There may be faster code that is equivalent to the posted code and thus is also correct. The goal is to find this code. One doesn't need to understand what the code does. – n. m. could be an AI Oct 01 '21 at 05:49
  • 1
    It isn't clear why you need the outer loop. Why not just go through i and j and run the body for each `initial_position_array[j][i]`? – n. m. could be an AI Oct 01 '21 at 05:53
  • @n.1.8e9-where's-my-sharem. Ah, but according to the question, the OP wants to do something like the un-optimized version. That leaves a bit of ambiguity, and I choose to interpret that as meaning the OP *wants* the inefficiencies. To avoid this ambiguity, the actual requirements should be spelled out, not letting me pick and choose from the code... – JaMiT Oct 01 '21 at 05:54
  • Also, "a lot of unnecessary iterations" --- what does it mean exactly? Which iterations are unnecessary? – n. m. could be an AI Oct 01 '21 at 05:56
  • @JaMiT running code is never rhe goal. Getting output is the goal. If you can get the same (or equivalent, depending) output, you are good. – n. m. could be an AI Oct 01 '21 at 06:00
  • 1
    Please include a call to the actual evaluation in each of the alternative snippets. One example each of the comparisons of the changed `if` becomes true and false might help - why are those good samples to export, anyway? Does the order of samples matter, considering the controlling numbers are "random"? What does `starts recording` refer to - all I see is taking another snapshot? – greybeard Oct 01 '21 at 07:03
  • It's unclear to me why you're using the outer loop. You can try hoisting the evaluation of `initial_position_array[j]` and `delta_time_arr[j]` out of the innermost loop, since they are both independent of `i`. Beyond that, it comes down to organisation of your data and choice of algorithm. You're essentially doing a brute-force search repeatedly by iterating from the beginning of the arrays each time. If there is a pattern to the values (e.g. sorted) you can probably use a smarter algorithm, eliminate at least a couple of the loops. – Peter Oct 01 '21 at 07:23

2 Answers2

1

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.

greybeard
  • 2,249
  • 8
  • 30
  • 66
0
 if (it == initial_position_array[j][i] || it == (initial_position_array[j][i] + delta_time_arr[j]))

you can use references for both expressions.(please use meaningful names as per your convinience)

int& i_p_a = initial_position_array[j][i];
int& i_p_a_d = (initial_position_array[j][i] + delta_time_arr[j]);

now you final if statement will be readable and maintainable.

if (it == i_p_a || it == i_p_a_d) {
     exportedStates = system.GetStates();
     Export2D(exportedStates, outputStatesFile, it);
} 
Jitu DeRaps
  • 144
  • 1
  • 9