Having started the debate on which is faster in my post Validation: CHAIN versus SETLL I decided to put the theory to the test.
I created a DDS file with a single key:
A UNIQUE A R TESTFILER A KEY 7P 0 A F1 3 A F2 5P 0 A F3 30 A F4 3P 2 A F5 50 A K KEY
And filled the file with a million records, the field KEY contained the values of 1 to 1 million.
I created two almost identical programs. Each one would perform their operation 1 million times, and write a record to an output file with the Start time, End time, and the number of milliseconds it took to perform the 1 million operations. Those programs are listed below:
01 FTESTFILE IF E K DISK 02 FTESTPF O E DISK 03 D KeyFld S like(KEY) 04 D TmeStmp S Z 05 D i S 10U 0 /free 06 PGM = 'CHAIN' ; 07 START = %timestamp() ; 08 for i = 1 by 1 to 1000000 ; 09 TmeStmp = %timestamp() ; 10 KeyFld = %subdt(TmeStmp:*ms) ; 11 chain KeyFld TESTFILER ; 12 endfor ; 13 FINISH = %timestamp() ; 14 DIFFERENCE = %diff(FINISH:START:*ms) ; 15 write TESTPFR ; 16 *inlr = *on ;
01 FTESTFILE IF E K DISK 02 FTESTPF O E DISK 03 D KeyFld S like(KEY) 04 D TmeStmp S Z 05 D i S 10U 0 /free 06 PGM = 'SETLL' ; 07 START = %timestamp() ; 08 for i = 1 by 1 to 1000000 ; 09 TmeStmp = %timestamp() ; 10 KeyFld = %subdt(TmeStmp:*ms) ; 11 setll KeyFld TESTFILER ; 12 endfor ; 13 FINISH = %timestamp() ; 14 DIFFERENCE = %diff(FINISH:START:*ms) ; 15 write TESTPFR ; 16 *inlr = *on ;
The only differences are:
- Line 6: Name written to the field in the output file.
- Line 11: The operation is performed.
A CL program was written to call each of these programs five times.
The CL was submitted to the QINTER job queue on an IBM i 8406 70Y on a Sunday afternoon. Being a holiday weekend I knew that there would be no-one else running jobs on this server. The results were:
This shows that in these programs where the operation was performed 1 million times the SETLL is 1.68 seconds faster than the CHAIN.
For one single operation the difference is negligible. But we all need to make our own decisions on when to use these kinds of performance differences, when to use one operation code rather than another, and what this conveys to others, or to are ourselves at a later date, when looking at the code.
Monday September 2: John Blenkinsop made an interesting comment about what would happen if there were unsuccessful SETLL and CHAIN operations?
I rebuilt the input file with the key field, KEY, containing the values of 1 to 1 million, but this time I incremented by 2, resulting in a file of 500,000 records.
I ran the same programs that I had before and these were the results:
The difference is even more significant, 9.08 seconds, in favor of SETLL.