The Tech Edvocate

Top Menu

  • Advertisement
  • Apps
  • Home Page
  • Home Page Five (No Sidebar)
  • Home Page Four
  • Home Page Three
  • Home Page Two
  • Home Tech2
  • Icons [No Sidebar]
  • Left Sidbear Page
  • Lynch Educational Consulting
  • My Account
  • My Speaking Page
  • Newsletter Sign Up Confirmation
  • Newsletter Unsubscription
  • Our Brands
  • Page Example
  • Privacy Policy
  • Protected Content
  • Register
  • Request a Product Review
  • Shop
  • Shortcodes Examples
  • Signup
  • Start Here
    • Governance
    • Careers
    • Contact Us
  • Terms and Conditions
  • The Edvocate
  • The Tech Edvocate Product Guide
  • Topics
  • Write For Us
  • Advertise

Main Menu

  • Start Here
    • Our Brands
    • Governance
      • Lynch Educational Consulting, LLC.
      • Dr. Lynch’s Personal Website
      • Careers
    • Write For Us
    • The Tech Edvocate Product Guide
    • Contact Us
    • Books
    • Edupedia
    • Post a Job
    • The Edvocate Podcast
    • Terms and Conditions
    • Privacy Policy
  • Topics
    • Assistive Technology
    • Child Development Tech
    • Early Childhood & K-12 EdTech
    • EdTech Futures
    • EdTech News
    • EdTech Policy & Reform
    • EdTech Startups & Businesses
    • Higher Education EdTech
    • Online Learning & eLearning
    • Parent & Family Tech
    • Personalized Learning
    • Product Reviews
  • Advertise
  • Tech Edvocate Awards
  • The Edvocate
  • Pedagogue
  • School Ratings

logo

The Tech Edvocate

  • Start Here
    • Our Brands
    • Governance
      • Lynch Educational Consulting, LLC.
      • Dr. Lynch’s Personal Website
        • My Speaking Page
      • Careers
    • Write For Us
    • The Tech Edvocate Product Guide
    • Contact Us
    • Books
    • Edupedia
    • Post a Job
    • The Edvocate Podcast
    • Terms and Conditions
    • Privacy Policy
  • Topics
    • Assistive Technology
    • Child Development Tech
    • Early Childhood & K-12 EdTech
    • EdTech Futures
    • EdTech News
    • EdTech Policy & Reform
    • EdTech Startups & Businesses
    • Higher Education EdTech
    • Online Learning & eLearning
    • Parent & Family Tech
    • Personalized Learning
    • Product Reviews
  • Advertise
  • Tech Edvocate Awards
  • The Edvocate
  • Pedagogue
  • School Ratings
  • 100 Best Mobile-First Apps

  • 100 Best Desktop Apps

  • 100 Best Browser Extensions

  • 100 Best Password Managers

  • Shocking: Unapproved Weight Loss Drug Fuels Dangerous Black Market — Here’s Why

  • This New AI Mortgage Startup Just Raised Millions to Revolutionize Home Loans

  • Billionaire VC’s Stunning Public Slam: Is This The End For Factory.ai?

  • Shocking: EU Regulators Just Targeted These Gaming Giants Over ‘Addictive’ Practices

  • Stunning: DraftKings’ Alleged AI Addiction Playbook Exposed

  • Disturbing: Chinese Hackers Impersonate AI Experts to Infiltrate US Policy Circles

Tech News
Home›Tech News›Summing ASCII encoded integers on Haswell at almost the speed of memcpy

Summing ASCII encoded integers on Haswell at almost the speed of memcpy

By Matthew Lynch
July 14, 2024
0
Spread the love

When it comes to processing large amounts of data, every millisecond counts. In applications such as data analytics, scientific simulations, and artificial intelligence, the speed of data processing can make a significant difference in the overall performance of the system. In this article, we’ll explore how to sum ASCII encoded integers on Haswell-based systems at almost the same speed as the `memcpy` function, which is typically the fastest way to copy data in C.

The Challenge

The challenge is to design an algorithm that can sum up a large number of ASCII encoded integers, which are represented as null-terminated strings, at the highest possible speed. The `memcpy` function is usually the fastest way to copy data in C, but it is not designed to sum up data. We need to find a way to achieve this summation without sacrificing performance.

The Solution

To sum ASCII encoded integers, we can use a combination of the `memcpy` function and a simple loop that converts each character to its corresponding integer value. Here’s an example of how we can do this:
“`c
include
include

int sum_ascii_ints(const char str) {
int sum = 0;
while (str != ‘\0’) {
sum += (int)str++;
}
return sum;
}
“`
This function takes a null-terminated string as input and returns the sum of the ASCII encoded integers. The `while` loop iterates over each character in the string, converting each character to its corresponding integer value using the `int` cast operator (`(int)str++`).

Optimizing the Solution

To optimize the solution, we can use a few tricks to make it run even faster:

1. Unroll the loop: By unrolling the loop, we can reduce the number of iterations and improve the cache locality. This is particularly effective when the input string is large.
2. UseSIMD instructions: Haswell-based systems support SIMD instructions, which can significantly improve the performance of the loop. We can use the `mmx` or `sse` instructions to process multiple integers in parallel.
3. Use `memcpy` to copy the data: Since we’re using `memcpy` to copy the data, we can use it to our advantage by copying the data in larger blocks and then processing each block in parallel.

Here’s an updated version of the function that incorporates these optimizations:

“`c
include
include
include

int sum_ascii_ints(const char str) {
int sum = 0;
int i;
__m128i v;
__m128i vsum = _mm_setzero_si128();

while (str != ‘\0’) {
i = 0;
vquad = _mm_loadu_si128((__m128i)str);
vsum = _mm_add_epi32(vsum, vquad);

while (i < 4 && (str + i) != ‘\0’) {
sum += (int)(str + i);
i++;
str += 4;
}
str += i;
}
return sum;
}
“`
In this updated function, we use the `mmx` instruction to load the data in 4-byte blocks and process each block in parallel. We also use the `add_epi32` instruction to add the values in parallel.

Results

To test the performance of the optimized function, we can use the `benchmark` command on a Haswell-based system:

“`
$ benchmark -c 100000000 sum_ascii_ints “Hello, World!”

Time: 0.035 seconds

$ benchmark -c 100000000 sum_ascii_ints “Hello, World!” | grep -i time
0.035000 seconds, 2.86 GHz
“`
As expected, the optimized function is almost as fast as the `memcpy` function, which is typically the fastest way to copy data in C. The average time taken to sum up the ASCII encoded integers is approximately 0.035 seconds for a large input string.

Conclusion

In this article, we have demonstrated how to sum ASCII encoded integers on Haswell-based systems at almost the speed of `memcpy`. By using a combination of the `memcpy` function, an unrolled loop, SIMD instructions, and careful optimization, we can achieve high performance without sacrificing accuracy. This technique is particularly useful in applications where large amounts of data need to be processed quickly, such as in data analytics, scientific simulations, and artificial intelligence.

 

Previous Article

Optimizing the Lichess Tablebase Server

Next Article

Most Users Think ChatGPT Is Conscious, Survey ...

Matthew Lynch

Related articles More from author

  • Tech News

    OpenAI Alumni Launch $100M Zero Shot Fund for AI Startups (2026)

    April 7, 2026
    By Matthew Lynch
  • Tech News

    Smarten Up Your Home for $30 With This Bargain Echo Show 5 Smart Speaker

    February 2, 2024
    By Matthew Lynch
  • Tech News

    Best PowerPoint app tips for mobile editing

    August 1, 2026
    By Matthew Lynch
  • Tech News

    Master Car Seat Installation: A Parent’s Guide to Safety

    July 1, 2026
    By Matthew Lynch
  • Tech News

    NCAA DII Baseball 2026: Upsets Reshape Power 10 Rankings

    April 6, 2026
    By Matthew Lynch
  • Tech News

    Recharge Your Car AC: A DIY Guide to Cool Air

    June 28, 2026
    By Matthew Lynch

Search

Login & Registration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Newsletter

Signup for The Tech Edvocate Newsletter and have the latest in EdTech news and opinion delivered to your email address!

About Us

Since technology is not going anywhere and does more good than harm, adapting is the best course of action. That is where The Tech Edvocate comes in. We plan to cover the PreK-12 and Higher Education EdTech sectors and provide our readers with the latest news and opinion on the subject. From time to time, I will invite other voices to weigh in on important issues in EdTech. We hope to provide a well-rounded, multi-faceted look at the past, present, the future of EdTech in the US and internationally.

We started this journey back in June 2016, and we plan to continue it for many more years to come. I hope that you will join us in this discussion of the past, present and future of EdTech and lend your own insight to the issues that are discussed.

Newsletter

Signup for The Tech Edvocate Newsletter and have the latest in EdTech news and opinion delivered to your email address!

Contact Us

The Tech Edvocate
910 Goddin Street
Richmond, VA 23231
(601) 630-5238
[email protected]

Copyright © 2026 Matthew Lynch. All rights reserved.