04-04-2016 02:16 PM
Hi all,
I would be grateful if someone can help me to solve this issue,
I have a FOR loop, this FOR loop gives me a sequence of numbers from zero to1 million (1000000) as mentioned in the attached file. the probelm is this for loop makes my code is too slow when I run the code. So is there anyone can help me to replace this for loop with something more efficient and gives me the same results. thanks in advance
Regards
04-04-2016 02:19 PM
What else do you have in your code? I would not expect this, as shown, to slow down your code. It should just compile down to a constant.
04-04-2016 02:21 PM - edited 04-04-2016 02:24 PM
You don't then the fact that you are creating an array with a million elements has anything to do with it do you?
What exactly are you trying to do?
Actually I just ran that code and it takes less than one second to make that 1,000,000 element array, so I guess that is not it...
04-04-2016 02:27 PM
the 1 million is just the number of smaples that I am using to represent the X-axis i the XY graph, so I was using this way to get that. but long time ago I was suffering from the code is being so slow and today by the chance I discovered that when I delet the for loop I got so fast code but offcourse with wrong results.
SO that's why I am trying to find out the alternative way to get ride of the FOR loop in my code.
04-04-2016 02:32 PM
@Al-rawachy wrote:the 1 million is just the number of smaples that I am using to represent the X-axis i the XY graph, so I was using this way to get that. but long time ago I was suffering from the code is being so slow and today by the chance I discovered that when I delet the for loop I got so fast code but offcourse with wrong results.
Why not just use a normal graph instead? Then you do not have to supply the X-axis and it will just be a sample count for the x-axis.
04-04-2016 02:47 PM
Under "Normal" graph crossrulz means waveform graph.
With default options, if you just wire Y array to its terminal, X-axis will be automatically a duplicate of your XY-graph (start = 0, step = 1).
04-04-2016 02:48 PM
I am using XY graph becasue I have to plot two different things on th same plot
04-04-2016 02:54 PM
We would really need to see more of your code to try to give suggestions. I ran a quick test with the code below and the average run time was around .006 seconds or so.
As someone mentioned above, the for loop should be constant folded if you have debugging disabled, although it didn't indicate that to me when I tried. Either way, .006 seconds would normally be unnoticable, are you by chance doing this array creation in a loop very quickly?
04-04-2016 03:15 PM
@Al-rawachy wrote:I am using XY graph becasue I have to plot two different things on th same plot
And what are these two different things? Does the second plot need the XY?
04-05-2016 02:34 AM
@Al-rawachy wrote:I am using XY graph becasue I have to plot two different things on th same plot
You can plot 2 waveforms in a waveform graph. If the other is of another sampling frequency it can be padded. Only if the 2nd is sporadic a XY graph is warrented.
Still, it shouldn't be that for loop that's the culprit.
/Y