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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:33:18+00:00 2026-05-27T15:33:18+00:00

So, I am about 99% certain that I have implemented something wrong, but heres

  • 0

So, I am about 99% certain that I have implemented something wrong, but heres the deal.

I have been playing around with Grand Central Dispatch, and put together an experiment calculating MD5 hashes. I am running a macbook air with an i5, so have 4 cores available. This would led me to believe that using Grand Central Dispatch to calculate the hashes would be approx 4 times faster. But, for some reason, it appears to be slower.

Code below

Using GCD

#include <stdio.h>
#include <time.h>
#import <CommonCrypto/CommonDigest.h>
#import <dispatch/dispatch.h>

int main (int argc, const char * argv[])
{

    int i,j,k,l,a;
    int num_chars = 4, total;
    clock_t start, end;
    double elap;

    printf("Calculating hashes for %d chars\n", num_chars);

    total = num_chars ^ 64;

    printf("Calculating %d hashes\n", total);

    dispatch_queue_t queue = dispatch_get_global_queue(0,0);
    dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_group_t group = dispatch_group_create();

    start = clock();

   printf("Starting calculation queue\n");
   for(i=0;i<62;i++) {
        for(j=0;j<62;j++) {
            for(k=0;k<62;k++) {
                for(l=0;l<62;l++) {

                    dispatch_group_async(group, queue, ^{

                        char *letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                        char buffer[10];
                        char out[100];
                        unsigned char hash[16];

                        sprintf(buffer, "%c%c%c%c", letters[i], letters[j], letters[k], letters[l]);
                        CC_MD5(buffer, strlen(buffer), hash); 

                    });


                }
            }
        }
    }

   printf("Finished calculation queue\n");
   dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

    end = clock();

    elap = ((double) end - start) / CLOCKS_PER_SEC;

    printf("Time taken  %2f\n", elap);
    return 0;
}

Compile and run…

gcc -o a.out main.c
./a.out
Calculating hashes for 4 chars
Calculating 68 hashes
Starting calculation queue
Finished calculation queue
Time taken  35.193133

Looking at Activity Monitor, I can see all 4 cores max out while the script is running.

Now, comment out the dispatching….

#include <stdio.h>
#include <time.h>
#import <CommonCrypto/CommonDigest.h>
#import <dispatch/dispatch.h>

int main (int argc, const char * argv[])
{

    int i,j,k,l,a;
    int num_chars = 4, total;
    clock_t start, end;
    double elap;

    printf("Calculating hashes for %d chars\n", num_chars);

    total = num_chars ^ 64;

    printf("Calculating %d hashes\n", total);

    dispatch_queue_t queue = dispatch_get_global_queue(0,0);
    dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_group_t group = dispatch_group_create();

    start = clock();

   printf("Starting calculation queue\n");
   for(i=0;i<62;i++) {
        for(j=0;j<62;j++) {
            for(k=0;k<62;k++) {
                for(l=0;l<62;l++) {

                    //dispatch_group_async(group, queue, ^{

                        char *letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                        char buffer[10];
                        char out[100];
                        unsigned char hash[16];

                        sprintf(buffer, "%c%c%c%c", letters[i], letters[j], letters[k], letters[l]);
                        CC_MD5(buffer, strlen(buffer), hash); 

                    //});




   }
        }
    }
}

printf("Finished calculation queue\n");
//dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

end = clock();

elap = ((double) end - start) / CLOCKS_PER_SEC;

printf("Time taken  %2f\n", elap);
return 0;
}

Compile and run

gcc -o b.out main.c
./b.out
Calculating hashes for 4 chars
Calculating 68 hashes
Starting calculation queue
Finished calculation queue
Time taken  7.511273

Looking at Activity Monitor, it only shows 1 core active while the script runs.

  • 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-27T15:33:18+00:00Added an answer on May 27, 2026 at 3:33 pm

    There’s probably too little work being done in the dispatch that it doesn’t make the overhead involved with dispatching worthwhile. I would try and increase the amount of work done in each dispatch. I wouldn’t have a clue whether this would help, but try:

    1. Moving the dispatch up a few loops, perhaps wrap the k or j loop inside the dispatched block instead, to get it to do more work.

    2. Remove the call sprintf and strlen. In fact, the block could be simplified to:

      static const char *letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      unsigned char hash[16];
      char buffer[4] = { letters[i], letters[j], letters[k], letters[l] };
      
      CC_MD5(buffer, sizeof buffer, hash);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I write an article or document about a certain topic, but the
I'm almost certain about the answer, but the situation is so critical that I
I have been reading a lot about test-driven development and decided that I want
OK I know that I may be thinking about this the wrong way but
What I'm talking about is: Is it possible that under certain circumstances the CPU
I'm building something which has a countdown to a certain date/time. I have it
I have a general question about rails controllers/models: I have a model Providers, that
I have a problem with Gridview sorting that is similar to others but I'm
I have been toying with PHP filter library. I liked it but I am
I am currently learning jQuery, and I am curious about something. For functions that

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.