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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:19:41+00:00 2026-06-08T00:19:41+00:00

I decided to benchmark reading an image in C#, and in C++, to decide

  • 0

I decided to benchmark reading an image in C#, and in C++, to decide which language to use in a project i’m thinking about making for myself.
I expected the benchmarks to be extremely close with C++ maybe pushing ahead slightly.

The C# code takes about 300ms each run (I ran each test 100 times), where the C++ code takes about 1.5ms.

So is my C# code wrong? Am I benchmarking it badly? Or is it really just this much slower?

Here’s the c# code I used:

Stopwatch watch = new Stopwatch();

watch.Start();
Image image = Image.FromFile(imagePath);
watch.Stop();

Console.WriteLine("DEBUG: {0}", watch.ElapsedMilliseconds);

And the C++ code pretty much boiled down to this:

QueryPerformanceCounter(&start);
Image * img = Image::FromFile(imagePath);
QueryPerformanceCounter(&stop);
delete img;
return (stop.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;

Regardless of which language, they need to end up in an Image object, as it provides the functionality i’m going to need.

=======================================================================

As xanatos pointed out in the comments, the Image.FromFile does do checking.

More specifically, this:

num = SafeNativeMethods.Gdip.GdipImageForceValidation(new HandleRef(null, zero));
if (num != 0)
{
    SafeNativeMethods.Gdip.GdipDisposeImage(new HandleRef(null, zero));
    throw SafeNativeMethods.Gdip.StatusException(num);
}

Using Image.FromStream() instead, you can avoid this.

What i’m wondering is, if you do avoid this and try to load an invalid image file it throws an OutOfMemory exception.
And in C++, you don’t do checking like this. So how important is this checking? Can anyone give me a situation where it would be bad to avoid 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-06-08T00:19:43+00:00Added an answer on June 8, 2026 at 12:19 am

    Yes, your benchmark is flawed. The problem is that you forgot to actually do something with the bitmap. Like paint it.

    GDI+ heavily optimizes the loading of an image. Very similar to the way .NET optimizes loading an assembly. It does the bare things necessary, it reads the header of the file to retrieve essential properties. Format, Width, Height, Dpi. Then it creates a memory-mapped file to create a mapping to the pixel data in the file. But doesn’t actually read the pixel data.

    Now the difference comes into play. System.Drawing.Image next actually reads the pixel data. That causes page faults, the operating system now reads the file and copies the pixel data into RAM. Highly desirable, if there’s anything wrong with the file then you’ll get an exception at the FromFile() call instead of some time later, typically when your program draws the image and is buried in framework code you didn’t write. Your bench mark for the C# code times the creation of the mmf plus the reading of the pixel data.

    The C++ program is always going to have to pay for reading the pixel data too. But you didn’t measure that, you only measured the cost of creating the MMF.

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

Sidebar

Related Questions

I decided to learn how to use github about a month ago, and I
I decided not to use maven in my project. So, I right-clicked it in
Decided to use Apache's Common Configuration package to parse an XML File. I decided
I decided not to use an orm and going to use straight ADO.NET for
I decided to learn Assembly language. The main reason to do so is being
I am looking for ways to perform micro-benchmarks on multi-core processors. Context: At about
Decided to move one of my project from iBatis to MyBatis and ran into
I decided to use UIScrollView in my application since I have to scroll the
We decided to use mongodb for some web application (instead of mysql) but want
I decided to try and use mod_rewrite to hide the location of a file

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.