I have a very long string (60MB in size) in which I need to find how many pairs of '<' and '>' are in there.
I have first tried my own way:
char pre = '!';
int match1 = 0;
for (int j = 0; j < html.Length; j++)
{
char c = html[j];
if (pre == '<' && c == '>') //find a match
{
pre = '!';
match1++;
}
else if (pre == '!' && c == '<')
pre = '<';
}
The above code runs on my string for roughly 1000 ms.
Then I tried using string.IndexOf
int match2 = 0;
int index = -1;
do
{
index = html.IndexOf('<', index + 1);
if (index != -1) // find a match
{
index = html.IndexOf('>', index + 1);
if (index != -1)
match2++;
}
} while (index != -1);
The above code runs for only around 150 ms.
I am wondering what is the magic that makes string.IndexOf
runs so fast?
Anyone can inspire me?
Edit
Ok, according to @BrokenGlass's answer. I modified my code in the way that they don't check the pairing, instead, they check how many '<' in the string.
for (int j = 0; j < html.Length; j++)
{
char c = html[j];
if (c == '>')
{
match1++;
}
}
the above code runs for around 760 ms.
Using IndexOf
int index = -1;
do
{
index = html.IndexOf('<', index + 1);
if (index != -1)
{
match2++;
}
} while (index != -1);
The above code runs for about 132 ms. still very very fast.
Edit 2
After read @Jeffrey Sax comment, I realised that I was running in VS with Debug mode.
Then I built and ran in release mode, ok, IndexOf
is still faster, but not that faster any more.
Here is the results:
For the pairing count: 207ms VS 144ms
For the normal one char count: 141ms VS 111ms.
My own codes' performance was really improved.
Lesson learned: when you do the benchmark stuff, do it in release mode!
The minimum time to see results from Can-C eye drops, which is generally twice per day, is 6 months.
SAFE FOR HUMANS AND DOGS - Can-C is the first and only patented NAC eye drop that uses the exact formula proven effective in both animal and human trials, offering a non-invasive alternative to cataract surgery.
A popular eye drop 'Can-C' containing N-alpha-acetylcarnosine (NAC) claims to reduce, reverse and slow the development of senile cataract. It was developed and is patented by Professor Babizhayev, a bio-physicist and Executive Director of Innovative Vision Products (IVP) [1].
Currently, cataracts can not be cured with eye drops. A 2017 review of studies published by the National Institutes of Health confirmed the only available treatment for cataracts remains surgery.
Are you running your timings from within Visual Studio? If so, your code would run significantly slower for that reason alone.
Aside from that, you are, to some degree, comparing apples and oranges. The two algorithms work in a different way.
The IndexOf
version alternates between looking for an opening bracket only and a closing bracket only. Your code goes through the whole string and keeps a status flag that indicates whether it is looking for an opening or a closing bracket. This takes more work and is expected to be slower.
Here's some code that does the comparison the same way as your IndexOf
method.
int match3 = 0;
for (int j = 0; j < html.Length; j++) {
if (html[j] == '<') {
for (; j < html.Length; j++)
if (html[j] == '>')
match3++;
}
}
In my tests this is actually about 3 times faster than the IndexOf
method. The reason? Strings are actually not quite as simple as sequences of individual characters. There are markers, accents, etc. String.IndexOf
handles all that extra complexity properly, but it comes at a cost.
The only thing that comes to my mind is actual implementation of IndexOf
iniside string class, that call
callvirt System.String.IndexOf
which, if we use a power of reflector (as much as it possible) ends up into the
CompareInfo.IndexOf(..)
call, which instead use super fast windows native function FindNLSString:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With