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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:16:52+00:00 2026-06-01T15:16:52+00:00

Ok, So, I just started screwing around with threading, now it’s taking a bit

  • 0

Ok, So, I just started screwing around with threading, now it’s taking a bit of time to wrap my head around the concepts so i wrote a pretty simple test to see how much faster if faster at all printing out 20000 lines would be (and i figured it would be faster since i have a quad core processor?)

so first i wrote this, (this is how i would normally do the following):

System.DateTime startdate = DateTime.Now;
    for (int i = 0; i < 10000; ++i)
    {
        Console.WriteLine("Producing " + i);
        Console.WriteLine("\t\t\t\tConsuming " + i);
    }

    System.DateTime endtime = DateTime.Now;
    Console.WriteLine(a.startdate.Second + ":" + a.startdate.Millisecond + " to " + endtime.Second + ":" + endtime.Millisecond);

And then with threading:

public class Test
{
static ProducerConsumer queue;
public System.DateTime startdate = DateTime.Now;
static void Main()
{
    queue = new ProducerConsumer();
    new Thread(new ThreadStart(ConsumerJob)).Start();


    for (int i = 0; i < 10000; i++)
    {
        Console.WriteLine("Producing {0}", i);
        queue.Produce(i);

    }
    Test a = new Test();
}

static void ConsumerJob()
{
    Test a = new Test();
    for (int i = 0; i < 10000; i++)
    {
        object o = queue.Consume();
        Console.WriteLine("\t\t\t\tConsuming {0}", o);

    }
    System.DateTime endtime = DateTime.Now;

    Console.WriteLine(a.startdate.Second + ":" + a.startdate.Millisecond + " to " + endtime.Second + ":" + endtime.Millisecond);
}
}

public class ProducerConsumer
{
readonly object listLock = new object();
Queue queue = new Queue();

public void Produce(object o)
{
    lock (listLock)
    {
        queue.Enqueue(o);        
        Monitor.Pulse(listLock);
    }
}

public object Consume()
{
    lock (listLock)
    {
        while (queue.Count == 0)
        {
            Monitor.Wait(listLock);
        }
        return queue.Dequeue();
    }
}



}

Now, For some reason i assumed this would be faster, but after testing it 15 times, the median of the results is … a few milliseconds different in favor of non threading

Then i figured hey … maybe i should try it on a million Console.WriteLine’s, but the results were similar

am i doing something wrong ?

  • 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-01T15:16:54+00:00Added an answer on June 1, 2026 at 3:16 pm

    Writing to the console is internally synchronized. It is not parallel. It also causes cross-process communication.

    In short: It is the worst possible benchmark I can think of 😉

    Try benchmarking something real, something that you actually would want to speed up. It needs to be CPU bound and not internally synchronized.

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

Sidebar

Related Questions

Just started using log4net and trying to get my head around the config and
Just started playing around with ice_cube I've got a weekly schedule (with a granularity
Just started exploring Cocoa so pretty much a total noob. I've written a very
Just started using Blueprint CSS and now playing with the grids but have a
Just started a tutorial in SQL for beginners. I'm doing some exercises now and
Just started to learn PostgreSQL 9.1 on linux through C and libpq. For now
Just started to play around with node.js, have some past experience with JavaScript but
Just started to experiment with HTML5 features and really like the localStorage. And now
Just started with Django but hit a bit of a wall - I decided
Just started playing around with, how is everyone linking up their component ids ?

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.