Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9046093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:33:19+00:00 2026-06-16T11:33:19+00:00

I have multiple same length arrays I wish to fill with zero’s. Let’s look

  • 0

I have multiple same length arrays I wish to fill with zero’s. Let’s look at two ways of doing that:

1)

int i;
for(i=0;i<ARRAYSLENGTH;i++){
    arr1[i]=0;
    arr2[i]=0;
    arr3[i]=0;
    ...
}

2) memset all the arrays to zero.

In a recent code review I was asked to change option 1 to option 2. Which made me wonder, Which of these methods is better ? Mainly:

Is 2 considered more readable than 1?

How do these two methods compare in terms of efficiency ? (considering memset is usually implemented in assembly but method 1 only increments the counter once for multiple arrays).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:33:21+00:00Added an answer on June 16, 2026 at 11:33 am

    Your method 1 (the for loop), is bad for the cache.

    As arr1, arr2, arr3 may not be anywhere near each other in memory, and very likely will not be in the cache together, you could have frequent cache-misses, and the CPU will have to constantly fetch new pieces from memory, just to set them to zero.

    By doing a set of memset operations, you’ll hit ALL of arr1, at once, almost certainly entirely from the cache. Then you’ll cache and set all of arr2 very quickly, etc, etc.

    That, and because memset may well have assembly tricks and optimizations to make it faster, I would definitely prefer option 2 over option 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple images that have the same class attribute and I want to
I have multiple ajax forms on page that cointain same elemets, I have jquery
I have multiple BIRT reports that obtains the data from the same jdbc data
Lets say that I have multiple functions that accomplish the same goal. An example
I have a page that makes the same JSONP request multiple times in a
I have multiple similar sites (same layout, just different data), and each of them
Is it considered bad practice to have multiple views for same URL in MVC,
I have multiple websites set up in the same folder, and I want to
I have multiple multi-select boxes with the same class and i want to unselect
I have multiple threads who all need to write to the same Dictionary. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.