Mike does a good job of illustrating how vector notation (operating on an entire dataset) can speed up scripts, but there are some problems with bjorn's script that should be pointed out.
The notation:
col(3)=col(2)(ii)*dt;
is referred to as 'mixed notation' because we are using vector notation on the left side and scalar notation on the right side. This would be very slow because Origin would assign the scalar value calculated on the right to every member of the vector (array) on the left with each pass through the loop - resulting in both the wrong answer and long delays. The correct notation should be:
col(3)[ii]=col(2)[ii]*dt;
Comparing all scripts:
sec;
for(ii=1;ii<1000;ii++)
{
dt=col(1)[ii]-col(1)[ii-1];
col(3)=col(2)[ii]*dt;
}
sec -e elapsed;
type Bjorn's Script takes $(elapsed) seconds;
type;
sec;
for(ii=1;ii<1000;ii++)
{
dt=col(1)[ii]-col(1)[ii-1];
col(3)[ii]=col(2)[ii]*dt;
}
sec -e elapsed;
type Bjorn's corrected script takes $(elapsed) seconds;
type;
sec;
copy -b 1 col(1) col(3) -b 2 -e 999;
col(3)*=-1;
col(3)+=col(1);
col(3)*=col(2);
sec -e elapsed;
type Mike's script takes $(elapsed) seconds;
type;
results in:
Bjorn's Script takes 2.319 secondsBjorn's corrected script takes 1.723 seconds
Mike's script takes 0.006 seconds
I should point out that Origin has a built-in function that finds the differences between rows of data, but since it offsets the results by one row it would yield a different result and since it internally creates a temporary dataset it doesn't have as much speed advantage as Mike's use of operator assignment notation which works 'in-place' on the data without the overhead of new memory allocation. Here is an example of the DIFF() function in use:
sec;
col(3)=diff(col(1));
col(3)*=col(2);
sec -e elapsed;
type My script takes $(elapsed) seconds;
type;
which results in:
My script takes 0.006 seconds
(See Note)
Again, I am not calculating the same values as Bjorn or Mike, and I only mean to illustrate the DIFF() function.
Note: Times on PII -233 system.