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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:59:07+00:00 2026-05-28T04:59:07+00:00

I wrote a small program using a custom indexOf function but wanted to dismiss

  • 0

I wrote a small program using a custom indexOf function but wanted to dismiss it in favour of the system string.IndexOf() method.

But before I started refactoring I wrote a small test program out of curiosity to see just see how bad my function was behaving in comparison of system string.IndexOf()

what I observed was the fact that system string.IndexOf seems to be a magnitude slower than iterating a array.

Processing random random string at 1000000 characters.
Processing method 1 system string.IndexOf
index 999999 took 620036 ticks
Processing method 2 custom IndexOf
index 999999 took 130007 ticks

So my question is really; Am I doing it wrong? Shouldn’t a system function much faster than anything else I write in c#?

tl;dr
the test scenario

first I set up some test data, filling a random string with some data

StringBuilder sb = new StringBuilder(); 
Random r = new Random(); 
string c = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
long before;
long after; 
for (int i = 0; i < 1000000; i ++) sb.Append(c[r.Next(c.Length)]);

and then insert something at the end of the array that I want to search for, worst case scenario

int j = sb.Length - 1 ;                                   
sb[j] = '"';

method 1: system string.IndexOf

and then check how many ticks is used by system string.IndexOf()

before = DateTime.Now.Ticks;
index = text.IndexOf("\"");
after = DateTime.Now.Ticks;

method 2:custom method

and after that I run my custom code which is just a static function with a while loop iterating over a character array.

before = DateTime.Now.Ticks; 
index = IndexOf(text, 0, '"', '/'); 
after = DateTime.Now.Ticks;
  • 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-28T04:59:07+00:00Added an answer on May 28, 2026 at 4:59 am

    I think the difference here is that when you’re calling String.IndexOf you’re using a string literal, and your custom function is using a character literal.

    Without having seen your custom implementation, I’d guess the String.IndexOf method is more correct (locales, unicode, and all that stuff).

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

Sidebar

Related Questions

So I wrote a project-management program for a small business using Microsoft Access 2007.
Here's a small test program I wrote: #include <iostream> using namespace std; class A
I'm trying to write a small baseball statistics program using data from retrosheet.org, but
I wrote a small program, that creates files at an interval of 1 minute.
I wrote a small program to test accessing a widget parent's slot. Basically, it
I wanted to write a small Rapid Serial Visual Presentation (RSVP) program for my
I wrote a small program in VS2005 to test whether C++ global operator new
So I wrote a small program to try out Boost Filesystem. My program will
I have been using ICC to compile a program I wrote for my research
I wrote a small C++ program in VS2k8. When I launch it from windows

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.