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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:23:32+00:00 2026-05-14T08:23:32+00:00

I was thinking about the speed difference of C++ to C# being mostly about

  • 0

I was thinking about the speed difference of C++ to C# being mostly about C# compiling to byte-code that is taken in by the JIT compiler (is that correct?) and all the checks C# does.

I notice that it is possible to turn a lot of these functions off, both in the compile options, and possibly through using the unsafe keyword as unsafe code is not verifiable by the common language runtime.

Therefore if you were to write a simple console application in both languages, that flipped an imaginary coin an infinite number of times and displayed the results to the screen every 10,000 or so iterations, how much speed difference would there be? I chose this because it’s a very simple program.

I’d like to test this but I don’t know C++ or have the tools to compile it. This is my C# version though:

static void Main(string[] args)
{
    unsafe
    {
        Random rnd = new Random();
        int heads = 0, tails = 0;
        while (true)
        {
            if (rnd.NextDouble() > 0.5)
                heads++;
            else
                tails++;

            if ((heads + tails) % 1000000 == 0)
                Console.WriteLine("Heads: {0}  Tails:  {1}", heads, tails);
        }
    }
}

Is the difference enough to warrant deliberately compiling sections of code “unsafe” or into DLLs that do not have some of the compile options like overflow checking enabled? Or does it go the other way, where it would be beneficial to compile sections in C++? I’m sure interop speed comes into play too then.

To avoid subjectivity, I reiterate the specific parts of this question as:

  • Does C# have a performance boost from using unsafe code?
  • Do the compile options such as disabling overflow checking boost performance, and do they affect unsafe code?
  • Would the program above be faster in C++ or negligably different?
  • Is it worth compiling long intensive number-crunching tasks in a language such as C++ or using /unsafe for a bonus? Less subjectively, could I complete an intensive operation faster by doing this?
  • 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-14T08:23:32+00:00Added an answer on May 14, 2026 at 8:23 am

    The example given is flawed because it does not show real life usage of both programming languages. Using simple datatypes to measure the speed of a language will not bring anything interesting. Instead, I suggest you create a template class in C++ and compare it with what is possible in C# for class generics. In the end, objects will bring some important results and you will see that C++ is faster than C#. Not to mention that you are comparing a lower level programming language with C#.

    Does C# have a performance boost from
    using unsafe code?

    Yes, it will have a boost but it is not suggested that you write only code with unsafe. Here is why: Code written using an unsafe context cannot be verified to be safe, so it will be executed only when the code is fully trusted. In other words, unsafe code cannot be executed in an untrusted environment. For example, you cannot run unsafe code directly from the Internet. http://msdn.microsoft.com/en-us/library/aa288474(VS.71).aspx

    Would the program above be faster in
    C++ or negligably different?

    Yes the program would be slightly faster in C++. C++ is a lower programming language and even faster if you start using the algorithm library (random_shuffle comes to mind).

    Is it worth compiling long intensive
    number-crunching tasks in a language
    such as C++ or using /unsafe for a
    bonus? Less subjectively, could I
    complete an intensive operation faster
    by doing this?

    It depends on the project…

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

Sidebar

Related Questions

Thinking about avoiding code replication, I got a question that catches me every time
I am thinking about reworking my GPU OpenCL kernel to speed things up. The
Just thinking about the best way to build an Order form that would (from
I am thinking about making a program that will need to send input and
So recently I started thinking about a way of improving speed (vs cpu usage)
Being bored earlier today I started thinking a bit about the relative performance of
Thinking about if we modify the definition of Hamiltonian path as we need to
Im thinking about to use a DB for my logs instead of a normal
Im thinking about building an application on our website to track order places through
Been thinking about this for hours now. Im building a simple slideshow application, where

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.