How to optimize the speed of execution ?

Hello,

I've implemented a little state machine consisting of "if...elseif..." statements, in order to display the right image according to the checkboxes which the user checks.
At the moment, there are 2 checkboxes : "Remove baseline" and "Interpolate"
The time of execution for each of these processings separately (time1 and time2) is quite good, but if the two checkboxes are checked, then I have to wait longer (Total_time > time1 + time2)

Would you know any easy way to solve this little issue ?

Thanks in advance,

Best regards

//------------------------ BASELINE CORRECTION --------------------------------------------------
Function CheckProc_RemoveBaseline(ctrlName, ckRemoveBaseline) : CheckBoxControl //If a click occurs on the "Remove baseline" checkbox
    String ctrlName
    variable ckRemoveBaseline

    SVAR STATE = root:STATE
   
    If (ckRemoveBaseline)
        if(cmpstr(STATE,"INTERP")==0) //if the image displayed is already interpolated
                SetActiveSubWindow My_Panel#G0                                                 
                Interpolate_Spectrum("ZSigPos","Interpolated_Spectrum")
                RemoveBaseline("Interpolated_Spectrum","W_Smooth","Interp_LOESS_BLcorrected")  
                RemoveImage Interpolated_Spectrum      
                AppendImage /W=My_Panel#G0 /T Interp_LOESS_BLcorrected vs {VcStep,PosTimepic}
                FormatGraph("My_Panel#G0","Interp_LOESS_BLcorrected","Geo",1)
                STATE = "BLCOR_INTERP"
        elseif (cmpstr(STATE,"RAW")==0) //if the image displayed is neither baseline-corrected, nor interpolated
                SetActiveSubWindow My_Panel#G0                                                 
                RemoveBaseline("ZSigPos", "W_Smooth", "ZsigPos_LOESS_BLcorrected")
                RemoveImage ZSigPos
                AppendImage /W=My_Panel#G0 /T ZsigPos_LOESS_BLcorrected vs {VcStep,PosTimepic}
                FormatGraph("My_Panel#G0","ZsigPos_LOESS_BLcorrected","Geo",1)
                STATE = "BLCOR"
           
            endif          
       
    Else    //if (ckRemoveBaseline==0) //if the user un-checks the "Remove baseline" option
        if (cmpstr(STATE,"BLCOR_INTERP")==0) //the current image is both interpolated and baseline-corrected
                SetActiveSubWindow My_Panel#G0
                RemoveImage Interp_LOESS_BLcorrected       
                AppendImage /W=My_Panel#G0 /T Interpolated_Spectrum vs {VcStep,PosTimepic}
                DisplayColorScale("Interpolated_Spectrum")                                         
                FormatGraph("My_Panel#G0","Interpolated_Spectrum","Rainbow",0)             
                STATE = "INTERP"   
        elseif(cmpstr(STATE,"BLCOR")==0) //the current image is only baseline-corrected
                SetActiveSubWindow My_Panel#G0
                RemoveImage ZsigPos_LOESS_BLcorrected      
                AppendImage /W=My_Panel#G0 /T ZSigPos vs {VcStep,PosTimepic}               
                DisplayColorScale("ZSigPos")                                                           
                FormatGraph("My_Panel#G0","ZSigPos","Rainbow",0)                               
                STATE = "RAW"  
        endif                  
    Endif
End


//------------------------ INTERPOLATION --------------------------------------------------

Function CheckProc_Interpolate(ctrlName, ckInterpolate) : CheckBoxControl                      
    String ctrlName
    variable ckInterpolate
   
    SVAR STATE = root:STATE
   
    If (ckInterpolate)
        if (cmpstr(STATE,"RAW")==0)
            SetActiveSubWindow My_Panel#G0
            Interpolate_Spectrum("ZSigPos","Interpolated_Spectrum")
            RemoveImage ZSigPos
            AppendImage /W=My_Panel#G0 /T Interpolated_Spectrum vs {VcStep,PosTimepic}
            //Duplicate /O Interpolated_Spectrum, My_Pos_Spectrum                      
            //KillWaves Interpolated_Spectrum
            FormatGraph("My_Panel#G0","Interpolated_Spectrum","Rainbow",0)
            STATE = "INTERP"
           
        elseif(cmpstr(STATE,"BLCOR")==0)
            SetActiveSubWindow My_Panel#G0
            Interpolate_Spectrum("ZSigPos","Interpolated_Spectrum")
            RemoveBaseline("Interpolated_Spectrum","W_Smooth","Interp_LOESS_BLcorrected")
            RemoveImage ZsigPos_LOESS_BLcorrected
            AppendImage /W=My_Panel#G0 /T Interp_LOESS_BLcorrected vs {VcStep,PosTimepic}
            //Duplicate /O ZsigPos_LOESS_BLcorrected, My_Pos_Spectrum                      
            //KillWaves LOESS_BLcorrected
            FormatGraph("My_Panel#G0","Interp_LOESS_BLcorrected","Geo",1)
            STATE = "BLCOR_INTERP" 
        endif          
       
//                                                         
    Else //if ckInterpolate==0
        if (cmpstr(STATE,"BLCOR_INTERP")==0)
            SetActiveSubWindow My_Panel#G0
            RemoveImage Interp_LOESS_BLcorrected
            AppendImage /W=My_Panel#G0 /T ZsigPos_LOESS_BLcorrected vs {VcStep,PosTimepic}
            FormatGraph("My_Panel#G0","ZsigPos_LOESS_BLcorrected","Geo",1)
            STATE = "BLCOR"
           
        elseif(cmpstr(STATE,"INTERP")==0)
            SetActiveSubWindow My_Panel#G0
            RemoveImage Interpolated_Spectrum
            AppendImage /W=My_Panel#G0 /T ZsigPos vs {VcStep,PosTimepic}
            FormatGraph("Myg_Panel#G0","ZsigPos","Rainbow",0)
            STATE = "RAW"      
        endif
    Endif
End
Hi Nasser,

First, I'm a bit confused what "two checkboxes are checked" means exactly. This doesn't mean to hit two check boxes at the same time, right? Do you mean the time lag when pressing the second checkbox while the first one is still processing?
But for now I would go like this: Ok, I don't now if this suits your taste in programming style, but I would merge these two functions into one for both checkboxes and then move redundant operations to key spots to minimize the function size. Especially SetActiveSubWindow My_Panel#G0 is called every time. And I don't know how all this works exactly, but just by glazing at it, I think 8 processing branches for a 4 choice matrix seems a bit much to me. For me it's always too confusing to spot optimizations in huge functions, so I try to shrink processes as much as possible and isolate bottlenecks. It's also easier for me to spot little pesky errors this way (in the pasted code is a "Myg_Panel#G0" at the bottom, but I assume that's not written this way in your procedure).

Best Regards
Hello Chozo,

Thank you for your answer

chozo wrote:
First, I'm a bit confused what "two checkboxes are checked" means exactly. This doesn't mean to hit two check boxes at the same time, right?"

"two checkboxes are checked" means "One of the 2 checkboxes is checked, then the user checks the other one".

chozo wrote:
But for now I would go like this: Ok, I don't now if this suits your taste in programming style, but I would merge these two functions into one for both checkboxes and then move redundant operations to key spots to minimize the function size.

Yes, I know. I'll do it.

But I don't understand why the time of execution of one function followed by another one is much greater than the sum of the separate times (timeTotal_time > t1 + t2)

Best Regards
Quote:
But I don't understand why the time of execution of one function followed by another one is much greater than the sum of the separate times (timeTotal_time > t1 + t2)


Perhaps you forgot a break statement so both are executing instead of one of them.

Try executing it from the command line.

Try setting a breakpoint in the control action procedure.

Try printing out the execution time. For example:
Variable timer = StartMSTimer
. . . do something here . . .
Variable elapsedSeconds = StopMSTimer(timer) / 1E6
Print elapsedSeconds

Hello,

hrodstein wrote:

Perhaps you forgot a break statement so both are executing instead of one of them.


That's what I wish : executing one function (Interpolate), immediately followed by another one (Remove baseline). It takes 38 s to perform these two functions.
I'm still looking at this. I'll post an answer if I find something.

Thank you.

Best Regards
Finally, I understand. It is so obvious !
Interpolating, then Correcting the baseline takes 38 seconds because the image is bigger, because of the interpolation.

So, it's sufficient to correct the baseline, then interpolate the image.