- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Why writing and calling function can significantly speed up the execution time? And what are the rule-of-thumb to write JSL for the most efficient time/space complexity?
Hi everyone,
One of my tasks is the do the reliability analysis, in which I do using Fit Life by X.
In doing so, I have two JSL scripts that output exactly the same results but have different coding approaches.
The first approach simply has several pieces of code cascading together. The pseudo code looks something like the following:
// Plot Fit Life by X #1
Fit Life by X(...);
// Plot Fit Life by X #2
Fit Life by X(...);
// Plot Fit Life by X #3
Fit Life by X(...);
// And so on
The second one has a single function that we call it repeated with different X, Y, and censor columns. The pseudo code looks something like the following:
// Function that contains Fit Life by X
reli_analysis = function(
{input parameters},
{local variables},
Fit Life by X(...);
);
// Call the above function multiple times
reli_analysis(input parameters set #1);
reli_analysis(input parameters set #2);
reli_analysis(input parameters set #3);
// And so on
What I experienced is that the second code is much faster than the first one, in the order of nearly 10 times faster. (My actual JSL with the real dataset have the execution time (measured through wall clock time) of ~17 seconds from Code #2 vs. ~170 seconds from Code #1)
I have three questions as follows:
1. Why calling the function is much faster than writing out all codes explicitly?
2. What are other practices/tips that I should generally do in order to ensure that my code have the most efficient time and/or space complexity?
3. I am not sure whether JSL has built-in timing functions like CPU time and wall clock time? I timed the code executions by using today() function, but it involves a lot of manual coding. In addition, the elapsed times calculated this way are always slightly different depending on whether there are other tasks being executed.
Thank you so much for your help.
JMP Version: 17.0
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Why writing and calling function can significantly speed up the execution time? And what are the rule-of-thumb to write JSL for the most efficient time/space complexity?
I would not expect that either. There is a tiny overhead for the function call, related to copying the parameters and setting up the local variables.
But... if there is a huge array involved, using more than half of the available memory, using a local variable in the function would make sure the memory is freed before it is needed again. Consider this JSL:
x = J(20000,20000,0);
...
x = J(20000,20000,0);
the first allocation needs 3GB of memory. The second allocation needs an additional 3GB before it can release the first one (when x is overwritten.) If x is a local in the function, rather than a global, the memory for x is freed when the local x is forgotten at the end of the function. If not, a 4GB machine will page the old x to disk while zeroing the memory for the new memory that will replace x. Then it will forget what it paged to disk.
If that sounds like what you are doing, try this to prove it:
x = J(20000,20000,0);
...
x=0; // forget the old allocation first
x = J(20000,20000,0);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Why writing and calling function can significantly speed up the execution time? And what are the rule-of-thumb to write JSL for the most efficient time/space complexity?
I would not expect that either. There is a tiny overhead for the function call, related to copying the parameters and setting up the local variables.
But... if there is a huge array involved, using more than half of the available memory, using a local variable in the function would make sure the memory is freed before it is needed again. Consider this JSL:
x = J(20000,20000,0);
...
x = J(20000,20000,0);
the first allocation needs 3GB of memory. The second allocation needs an additional 3GB before it can release the first one (when x is overwritten.) If x is a local in the function, rather than a global, the memory for x is freed when the local x is forgotten at the end of the function. If not, a 4GB machine will page the old x to disk while zeroing the memory for the new memory that will replace x. Then it will forget what it paged to disk.
If that sounds like what you are doing, try this to prove it:
x = J(20000,20000,0);
...
x=0; // forget the old allocation first
x = J(20000,20000,0);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Why writing and calling function can significantly speed up the execution time? And what are the rule-of-thumb to write JSL for the most efficient time/space complexity?
...and hptime() is the microsecond function. (high performance? not sure what hp stands for.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Why writing and calling function can significantly speed up the execution time? And what are the rule-of-thumb to write JSL for the most efficient time/space complexity?
Another possibility: if the previous chart is open and the data table is being changed to make the new chart, the old chart may be responding to each change to the data table. Maybe the function version closes the old chart sooner?