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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:38:00+00:00 2026-06-13T23:38:00+00:00

I need to convert fast the string in format HHmmss to DateTime or integers.

  • 0

I need to convert fast the string in format “HHmmss” to DateTime or integers. I’ve tested such code:

Console.WriteLine("decoding " + text);
long microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("start time " + microseconds);
field = DateTime.ParseExact(text, "HHmmss", null);
microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("finish time " + microseconds);

and the output is

decoding 172400
start time 121
finish time 244

decoding 172400
start time 236
finish time 383

decoding 172400
start time 116
finish time 416

decoding 172400
start time 235
finish time 421

decoding 172359
start time 149
finish time 323

so in average about 150 microseconds. What’s a lot of time, i’m writing HFT software and the best HFT has in average 10 microseconds “tick-to-trade” time (this includes everything!). I understand that using c# this is imposible however i still think that 150 microseconds is too much even using c#.

Now I want to use another algorithm, however I don’t know how to “extract” integers from the text:

field = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, /*extract hour, min, sec from text*/)

What can you suggest and what would be the fastest way?
Please do not ask why I’m care about perfomance instead just suggest how to do that faster.

Results:

Using DateTime.ParseExact(text, "HHmmss", null) about 6-8 ticks

Using TimeSpan ts = TimeSpan.ParseExact(text, "hhmmss", null); about 3-4 ticks

Using int hour = 10 * text[0] + text[1] - 11 * '0';… about 0 ticks

Acutally much less than 0 ticks if using loop for measurements. Actually it was found that last version is 100 times faster than other.

Code:

    long startMicroseconds = sw.ElapsedTicks /*/ (Stopwatch.Frequency / (1000L * 1000L))*/;

    //TimeSpan ts = TimeSpan.ParseExact(text, "hhmmss", null);

    //int hour = 10 * text[0] + text[1] - 11 * '0';
    //int minute = 10 * text[2] + text[3] - 11 * '0';
    //int second = 10 * text[4] + text[5] - 11 * '0';

    field = DateTime.ParseExact(text, "HHmmss", null);

    long finishMicroseconds = sw.ElapsedTicks /*/ (Stopwatch.Frequency / (1000L * 1000L))*/;
    Console.WriteLine("elappsed " + (finishMicroseconds - startMicroseconds));
  • 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-13T23:38:01+00:00Added an answer on June 13, 2026 at 11:38 pm

    This approach doesn’t use any string substring or parsing methods. It uses only indexing and simple arithmetic:

    int hour   = (s[0] - '0') * 10 + s[1] - '0';
    int minute = (s[2] - '0') * 10 + s[3] - '0';
    int second = (s[4] - '0') * 10 + s[5] - '0';
    

    This next version is probably even faster because the calculation has been partially evaulated to help the compiler. As a result it is slightly harder to read and understand:

    int hour   = s[0] * 10 + s[1] - '0' * 11;
    int minute = s[2] * 10 + s[3] - '0' * 11;
    int second = s[4] * 10 + s[5] - '0' * 11;
    

    For kicks you might also want to see if this is even faster, though I suspect that this code will be the same as the previous version:

    int hour   = s[0] * 10 + s[1] - 528;
    int minute = s[2] * 10 + s[3] - 528;
    int second = s[4] * 10 + s[5] - 528;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert a Joda-Time DateTime into a String, in the following format:
I need to convert a String to enum. I followed the idea in String
I need to convert a string vector in a simple string. I do not
I need to convert full state name to its official state address code. For
I need to convert keys from type string to hash. The name of all
I need to convert very large text files (usually over 100 MB) of character
Is there any fairly fast php code to convert a city + country to
I need to write some code to convert an array of quads into a
I need to take a BufferedImage and convert it to YCbCr format so that
I'm looking for C# code to convert an HTML document to plain text. I'm

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.