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

  • SEARCH
  • Home
  • 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 7505249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:50:51+00:00 2026-05-29T21:50:51+00:00

I am looking for an efficient way to convert a large int[] into a

  • 0

I am looking for an efficient way to convert a large int[] into a string[] of csv strings where each csv is limited to a maximum of 4000 characters. The values in the array could be anything between 1 and int.MaxValue.

Here is my final code:

public static string[] GetCSVsFromArray(int[] array, int csvLimit)
{
    List<string> parts = new List<string>();
    StringBuilder sb = new StringBuilder();
    foreach(int id in array)
    {
        string intId = id.ToString();
        if (sb.Length + intId.Length < csvLimit)
            sb.Append(intId).Append(",");
        else
        {
            if (sb.Length > 0)
                sb.Length--;
            parts.Add(sb.ToString());
            sb.Length = 0;
        }
    }
    if(sb.Length>0)
       parts.Add(sb.ToString());
    return parts.ToArray();
}

Is there a more efficient way to do this?

So here is what I am now using (I was able to change the return parameter to the List type to save the ToArray() call at the end):

public static List<string> GetCSVsFromArray(int[] array, int csvLimit)
{
    List<string> parts = new List<string>();
    StringBuilder sb = new StringBuilder();
    foreach(int id in array)
    {
        string intId = id.ToString();
        if (sb.Length + intId.Length < csvLimit)
            sb.Append(intId).Append(",");
        else
        {
            if (sb.Length > 0)
                sb.Length--;
            parts.Add(sb.ToString());
            sb.Length = 0;
        }
    }
    if(sb.Length>0)
       parts.Add(sb.ToString());
    return parts;
}

Performance results:

10,000,000 items csv Limit of 4000 characters

  • Original: 2,887.488ms
  • GetIntegerDigitCount: 3105.355ms
  • Final: 2883.587ms

Whilst I only saved 4ms removing the ToArray() call on my developer machine this seems to make a significant difference on a much slower machine (saved over 200ms on a DELL D620)

  • 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-05-29T21:50:54+00:00Added an answer on May 29, 2026 at 9:50 pm

    You are doing a lot of heap memory allocations when creating a new string for each number just to calculate number of digits. Use following method to calculate number of digits in the number (see method below).

    So instead of

    string intId = id.ToString();
    if (sb.Length + intId.Length < csvLimit)
    

    Just use:

    if (sb.Length + this.GetIntegerDigitCount(id) < csvLimit)
    

    Results:

    • 2 times faster on 10 million numbers
    • Old: 4316ms, New:1983ms, Diff: 2333ms. Faster 217.6%

    EDIT: More results on large csv limit

    Items:10000000; csvLimit:4000; Old:2091ms, New:1868ms, Diff:223ms
    faster = 111.937901498929%


    Code I’ve used to measure time:

     double elapsedOld = 0;
     double elapsedNew = 0;
     int count = 10000000;
     int csvLimit = 4000;
     var items = Enumerable.Range(0, count).ToArray();
     var watch = Stopwatch.StartNew();
     this.GetCsVsFromArray(items, csvLimit);
     watch.Stop();
     elapsedOld = watch.ElapsedMilliseconds;
    
     watch = Stopwatch.StartNew();
     this.GetCsVsFromArrayTuned(items, csvLimit);
     watch.Stop();
     elapsedNew = watch.ElapsedMilliseconds;
     var stat = String.Format(
         "Items:{0}; csvLimit:{1}; Old:{2}ms, New:{3}ms, Diff:{4}ms faster = {5}%",                
         count,
         csvLimit,
         elapsedOld,
         elapsedNew,
         elapsedOld - elapsedNew,
         elapsedOld * 100 / elapsedNew);
    

    GetIntegerDigitCount:

    public int GetIntegerDigitCount(int valueInt)
    {
        double value = valueInt;
        int sign = 0;
        if (value < 0)
        {
            value = -value;
            sign = 1;
        }
    
        if (value <= 9)
        {
            return sign + 1;
        }
    
        if (value <= 99)
        {
            return sign + 2;
        }
    
        if (value <= 999)
        {
            return sign + 3;
        }
    
        if (value <= 9999)
        {
            return sign + 4;
        }
    
        if (value <= 99999)
        {
            return sign + 5;
        }
    
        if (value <= 999999)
        {
            return sign + 6;
        }
    
        if (value <= 9999999)
        {
            return sign + 7;
        }
    
        if (value <= 99999999)
        {
            return sign + 8;
        }
    
        if (value <= 999999999)
        {
            return sign + 9;
        }
    
        return sign + 10;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a simple, efficient way to convert strings in CamelCase to underscore
I'm looking for an efficient way to chance a string such that all sequences
While looking into Efficient way to compute p^q (exponentiation), where q is an integer
I'm looking for an efficient way to convert axes coordinates to pixel coordinates for
I am looking for an efficient way to pull the data I want out
I'm looking for an efficient way to get the list of unique commit authors
I am looking for a most efficient way to re-use widgets with different selectors...
I'm looking for an efficient algorithm for scrambling a set of letters into a
I'm looking for an efficient way to implement a serialization mechanism in C. I
I am looking for an efficient way to calculate the traffic speed for my

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.