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 6209629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:01:15+00:00 2026-05-24T06:01:15+00:00

I need to calculate how many times every character occurs in the given string.

  • 0

I need to calculate how many times every character occurs in the given string. I need to do it on C or C++, I can use any library. The problem is that I am not a C/C++ developer, so I am not sure that my code is optimal. I want to get the best performance algorithm, it is the main reason for this question.

I am using the following code at the moment:

using namespace std;
...

char* text;        // some text, may be very long
int text_length;   // I know this value, if it can help

map<char,int> table;
map<char,int>::iterator it;

for(int i = 0; c = text[i]; i++) {
    it = table.find(c);
    if (it2 == table.end()) {
        table[c] = 1;
    } else {
        table[c]++;
    }
}

I may use any other structure except std::map, but I do not know which structure is better.

Thanks for your help!

  • 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-24T06:01:16+00:00Added an answer on May 24, 2026 at 6:01 am

    You are doing it right using bucket sort. There cannot be a faster (non-parallel) algorithm for counting elements in a finite universe (such as characters).

    If you only use ASCII characters, you can use a simple array int table[256] to avoid the overhead of C++ containers.

    Using Duff’s device (which is actually slower on some CPUs nowadays):

    int table[256];
    memset(table, 0, sizeof(table));
    int iterations = (text_length+7) / 8;
    switch(count % 8){
        case 0:      do {    table[ *(text++) ]++;
        case 7:              table[ *(text++) ]++;
        case 6:              table[ *(text++) ]++;
        case 5:              table[ *(text++) ]++;
        case 4:              table[ *(text++) ]++;
        case 3:              table[ *(text++) ]++;
        case 2:              table[ *(text++) ]++;
        case 1:              table[ *(text++) ]++;
                     } while(--iterations > 0);
    }
    

    Update: As MRAB remarked, processing text chunks in parallel might give you a perfomance boost. But be aware that creating a thread is quite expensive, so you should measure, what the lowest amount of characters is, which justifies the thread creation time.

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

Sidebar

Related Questions

I need to calculate how many times each keyword is reoccurring in a string,
I need to count how many times some specific days appears between two given
I need to calculate the distance (in meters and miles) between two coordinates given
Edit: See Calculate how many minutes used in a given hour for how I
I wanted to use boost accumulators to calculate statistics of a variable that is
I need to calculate the number of seconds passed since the beginning of the
I need to calculate Math.exp() from java very frequently, is it possible to get
I need to calculate averages, standard deviations, medians etc for a bunch of numerical
I need to calculate the number of FULL month in SQL, i.e. 2009-04-16 to
I need to calculate date (year, month, day) which is (for example) 18 working

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.