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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:04:53+00:00 2026-05-13T08:04:53+00:00

After reading a question here about what things our computer could do in one

  • 0

After reading a question here about what things our computer could do in one second I made a little test I had in mind for a while and I’m very surprised by the results. Look:

Simple program to catch a null exception, takes almost one second to do 1900 iterations:

for(long c = 0; c < 200000000; c++)
{
    try
    {
        test = null;
        test.x = 1;
    }
    catch (Exception ex)
    {
    }
}

Alternatively, checking if test == null before doing the assignation, the same pogram can do aprox 200000000 iterations in one second.

for(long c = 0; c < 1900; c++)
{
    test = null;
    f (!(test == null))
    {
        test.x = 1;
    }
}

Anyone has a detailed explanation on why this HUGE diference ?

EDIT: Running the test in Release mode, outside Visual studio i’m getting 35000-40000 iterations vs 400000000 iterations (always aprox)

Note I’m running this with a crappy PIV 3.06Ghz

  • 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-13T08:04:54+00:00Added an answer on May 13, 2026 at 8:04 am

    There’s no way that should take a second for 1900 iterations unless you’re running in the debugger. Running performance tests under the debugger is a bad idea.

    EDIT: Note that this isn’t a case of changing to the release build – it’s a case of running without the debugger; i.e. hitting Ctrl-F5 instead of F5.

    Having said that, provoking exceptions when you can avoid them very easily is also a bad idea.

    My take on the performance of exceptions: if you’re using them appropriately, they shouldn’t cause significant performance issues unless you’re in some catastrophic situation anyway (e.g. you’re trying to make hundreds of thousands of web service calls and the network is down).

    Exceptions are expensive under debuggers – certainly in Visual Studio, anyway – due to working out whether or not to break into the debugger etc, and probably doing any amount of stack analysis which is unnecessary otherwise. They’re still somewhat expensive anyway, but you shouldn’t be throwing enough of them to notice. There’s still stack unwinding to do, relevant catch handlers to find, etc – but this should only be happening when something’s wrong in the first place.

    EDIT: Sure, throwing an exception is still going to give you fewer iterations per second (although 35000 is still a very low number – I’d expect over 100K) because you’re doing almost nothing in the non-exception case. Let’s look at the two:

    Non-exception version of the loop body

    • Assign null to variable
    • Check whether variable is null; it is, so go back to the top of the loop

    (As mentioned in the comments, it’s quite possible that the JIT will optimise this away anyway…)

    Exception version:

    • Assign null to variable
    • Dereference variable
      • Implicit check for nullity
      • Create an exception object
      • Check for any filtered exception handlers to call
      • Look up the stack for the catch block to jump to
      • Check for any finally blocks
      • Branch appropriately

    Is it any wonder that you’re seeing less performance?

    Now compare that with the more common situation where you do a whole bunch of work, possibly IO, object creation etc – and maybe an exception is thrown. Then the difference becomes a lot less significant.

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

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're calling putchar() with a double as an argument. It's… May 13, 2026 at 10:51 pm
  • Editorial Team
    Editorial Team added an answer I don't think you do - what I did to… May 13, 2026 at 10:51 pm
  • Editorial Team
    Editorial Team added an answer The bad thing about polling is, it can become expensive… May 13, 2026 at 10:51 pm

Related Questions

After reading this post I kinda felt in the same position as the guy
After spending some hours reading about Unit Testing and test driven development here on
I have a live 16-bit gray-scale video stream that is pushed through a ring-buffer
Just read this interesting article by Omar on his blog Linq to SQL solve
I'm trying to get more into LINQ-to-XML, so I've made myself a neat little

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.