skip to Main Content

Dev Tip: Using the REDUCE operator instead of SUM will improve the performance of your ABAP code.

In case you’ve missed our dev tips in past weeks, we will be posting them in the blog moving forward. To sign up and get your dev tips delivered to your inbox each week, click here!

Tip of the Week

Using the REDUCE operator instead of SUM will improve the performance of your ABAP code.
In the development world, time matters. Too often we focus on development time rather than program runtime. Deadlines certainly matter, but if your program performs so poorly that it’s unusable, the project will be a failure. This week’s tip will help you extract column totals from internal tables in a more efficient manner.

For years, ABAPers were required to loop through internal tables and use the SUM operator to get column totals. Starting with ABAP 7.40 SP08, a new operator called REDUCE was added.

The setup:
I created an internal table of student names and grades. You can do this however you’d like – I used another new ABAP 7.40 trick – the FOR loop.

This is the structure of internal table t_grades1:
I now have an internal table of 100 students with grades from 1 to 100 to play with.
To prove out the benefit of REDUCE over SUM, I created two FORMs.  One contains the code for SUM and the other for REDUCE.
Just as a side note, doesn’t the REDUCE code look cooler?

Let’s test this out though.  Is SUM or REDUCE better?

Ok, REDUCE wins, but 20 microseconds isn’t much.  Let’s up the record count from 100 to 1000.
Now we’re seeing the difference.  Let’s up it to 50000 records!
Honestly speaking, 6500 microseconds is less time than it takes to blink your eye.  In the computing world, we can’t think of terms of one iteration.  What if you were able to save this execution time 10 times in a program and that program was executed hundreds of times per day?  The cost savings add up quickly!
If you have an interest in viewing similar content, visit our blog, here

View our LinkedIn, here

Mike Berg is a Senior SAP Developer at Mindset. He leverages his 18+ years of experience across various SAP technologies and functional areas to bring optimal solutions for customers to light. Mike focuses on bringing the best user experience possible to users, which in turn maximizes ROI for the organization. He does this by emphasizing the correct technology, be it Fiori, Personas, or other, and by optimizing application performance and ease-of-use. Mike is a regular contributor to Mindset’s blog and development tips newsletter, as well as a speaker at SAP Sapphire and ASUG events.

Back To Top