26
Thu, Dec
0 New Articles

Performance When Converting Legacy Dates

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
Converting legacy dates to true dates? One approach is significantly faster.

 

Often, I need to convert from legacy dates to true dates. I convert mostly so I can easily do date arithmetic, but sometimes just for readability in ad hoc queries over old files. I'm a big fan of true dates and preach their usage when I get the chance, but there is some CPU overhead. This article is about improving efficiency in converting to true dates and reducing some of that overhead.

 

I know that discussing performance can start arguments that can become more heated and contentious that a political or religious debate. I don't want that to happen. So I'm just going to show you alternate code approaches and discuss how long each takes to run. Then you decide which the better approach is. Me, I'll go for the slightly shorter code, which also just happens to run significantly faster, but I'm getting ahead of myself.

 

One thing I've learned about legacy date fields is that occasionally they don't contain dates. Surprise! Trying to convert a date that is invalid causes a run-time check in RPG, usually around 2:00 a.m. Typically, good defensive programming would go something like this:

 

Use the TEST opcode to check if the legacy field is valid

If valid

      convert legacy field to true date

else

      logic to handle invalid data

endif

 

This is the kind of logic I was coding, using the TEST opcode to check that the date was valid. It turns out that there is a slightly shorter code structure that turns out to be faster.

 

I wrote short, CPU-intensive programs to run one million iterations of each code approach and compared the total CPU usage to get a relative difference in performance.

Alternative Approaches

Here's the code for DATCHK1, the one that uses the TEST opcode.

 

     D DATCHK1         pr

     d DATCHK1         pi

     d NumDate         s              6p 0 inz(080229)

     d wkDate          s               d

     d i               s             10i 0

      /FREE

       for i = 1 to 1000000;

           test(de) *YMD NumDate;

           if not %error;

               wkDate = %DATE(NumDate:*YMD);

           else;

               // Handle bad date

           endif;

       endfor;

       *inlr = *on;

       return;

      /END-FREE

 

DATCHK1 takes the negative approach, checking for valid data before it performs the conversion. I ran it three times after hours on a lightly used development box at V5R3. In all three runs, it used 21 CPU seconds. Your mileage may vary, depending on your hardware.

 

The second program, DATCHK2 uses the MONITOR opcode.

 

     D DATCHK2         pr

     d DATCHK2         pi

     d NumDate         s              6p 0 inz(080229)

     d wkDate          s               d

     d i               s             10i 0

      /FREE

       for i = 1 to 1000000;

           monitor;

               wkDate = %DATE(NumDate:*YMD);

           on-error;

               // Handle bad date

           endmon;

       endfor;

       *inlr = *on;

       return;

      /END-FREE

 

DATCHKC2 takes the positive approach, assuming that the data is valid and immediately trying to convert it and trapping any failure. I ran DATCHK2 three times on the same development box. Two runs each used 12 CPU seconds and one used 11 CPU seconds.

Which Is Faster? And Why?

If we go with 12 seconds, DATCHK2 is about 43 percent faster than DATCHK1 ((21-12)/21*100). If you look at it the other way, using TEST takes 75 percent longer than using MONITOR ((21-12)/21*100). You may be able to work these numbers another way, but they clearly show that the second approach is significantly faster.

 

Why is the MONITOR approach faster? Or why is the TEST approach slower? I don't think it's poor coding on the part of the compiler programmers. I suspect, rather, that when you convert to a legacy date, the conversion routines insist on valid input and probably go through the same validity checking that the TEST opcode goes through. So when you use the TEST opcode approach, the validity checking is being done twice, and this validity checking is costly in terms of CPU time (but cheap in terms of happy, satisfied customers, not to mention frazzled developers who don't get called in at 2:00 a.m. with an exception due to an invalid date.)

Is It Worth It?

I have a colleague whose rule of thumb is "first make it right; then make it fast." Sometimes we developers overcomplicate programs, trying to tease out a couple more microseconds per record, when the number of records being processed is so low that we maybe save a CPU second a day.  I'm not advocating sloppy, inefficient code, but we can sometimes get hung up on efficiency.

 

So why am I suggesting a differing code style that might save 10 CPU seconds per million iterations? It's a combination of several things:

  1. The MONITOR approach is one line shorter.
  2. The performance improvement is a nice bonus at no extra cost to complexity.
  3. I like the positive approach of assuming that the date is correct. Much as we may disparage legacy date fields, most of the time they do contain valid date values. 

The MONITOR positive approach has other applications when editing and converting data, and it's well worth becoming familiar with it. For more information, check it out in the ILE RPG Language Reference manual.

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: