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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:57:35+00:00 2026-05-23T20:57:35+00:00

I need to have very high-performance loop going over large datasets. I need to

  • 0

I need to have very high-performance loop going over large datasets. I need to compare TimeStamps, but my dates are in OA format:

if (md.DT_OA > new DateTime(2011, 3, 13).ToOADate()) 

Will the compiler evaluate new DateTime(2011, 3, 13).ToOADate() in each loop cycle? Or will the “optimiser” work this out once at beginning.

i.e Can I get away with being lazy and having this in the code?

As you can tell – I don’t know much about how compiler works…

EDIT 1

Comments inspired me do do a proper test:

Option 2 below is about 3% faster than Option 1. Which is surprising not MUCH faster – the compiler seems to be very smart or date creation is fast.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Go();
        }

        public static void Go() 
        {
            int c = 0;
            DateTime st = DateTime.Now;
            DateTime dt = new DateTime(2011, 3, 13);
            for (int i = 0; i < 1000000; i++) 
            {
                if (DateTime.Now > new DateTime(2011, 3, 13)) // Option 1
                //if (DateTime.Now > dt)                          // Option 2
                {
                    c++;
                }
            }

            MessageBox.Show("Time taken: " + DateTime.Now.Subtract(st).TotalMilliseconds + " c: " + c);
        } 
    }
}
  • 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-23T20:57:36+00:00Added an answer on May 23, 2026 at 8:57 pm

    I see no reason it would optimize that… pretty sure it won’t (I know it won’t optimize it at the IL layer, but that doesn’t say anything about the JIT; but I really don’t think it will – it doesn’t know that the method is always going to return the same thing each time).

    Instead, lift it out of the loop if you care about it:

    var oaDate = new DateTime(2011, 3, 13).ToOADate();
    ...loop...
        if (md.DT_OA > oaDate) {...}
    

    Re just new DateTime(int,int,int) (comments); let’s create a test program:

        static void Main()
        {
            for (int i = 0; i < 1000; i++)
            {
                if (DateTime.Now > new DateTime(2011, 3, 13)) Console.WriteLine("abc");
            }
        }
    

    if we compile that and then disassemble the IL (reflector/ildasm/etc), we get:

    L_0000: ldc.i4.0 
    L_0001: stloc.0 
    L_0002: br.s L_002b
    L_0004: call valuetype [mscorlib]System.DateTime [mscorlib]System.DateTime::get_Now()
    L_0009: ldc.i4 0x7db
    L_000e: ldc.i4.3 
    L_000f: ldc.i4.s 13
    L_0011: newobj instance void [mscorlib]System.DateTime::.ctor(int32, int32, int32)
    L_0016: call bool [mscorlib]System.DateTime::op_GreaterThan(valuetype [mscorlib]System.DateTime, valuetype [mscorlib]System.DateTime)
    L_001b: brfalse.s L_0027
    L_001d: ldstr "abc"
    L_0022: call void [mscorlib]System.Console::WriteLine(string)
    L_0027: ldloc.0 
    L_0028: ldc.i4.1 
    L_0029: add 
    L_002a: stloc.0 
    L_002b: ldloc.0 
    L_002c: ldc.i4 0x3e8
    L_0031: blt.s L_0004
    L_0033: ret 
    

    Look at L_0009 thru L_0011 – that is creating the new DateTime per loop iteration (L_0031: blt.s L_0004 is the loop repeat). The compiler has been very literal with your request, as I would expect.

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

Sidebar

Related Questions

We have some very large data files (5 gig to 1TB) where we need
I have a need for a high-performance string hashing function in python that produces
We have a very high performance multitasking, near real-time C# application. This performance was
i have very simple problem. I need to create model, that represent element of
I have a very common situation. I have a file, and I need to
I have a very simple entry form. After a button is pressed, I need
Ok, I have a very frustrating problem. I am parsing a webpage and need
I need a very quick fix in a mod_rewrite expression for drupal we have
I have 2 tables in a DB with very similar schemas that need to
I have trouble using sed. I need to replace some lines in very deprecated

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.