By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

find character in string speed (synchronous) problem

P: 2
hi all,

this is my code (in interrupt function) for finding character in string (data).

Expand|Select|Wrap|Line Numbers
  1. Ekle_text.Text += data;
  2. string veri = Ekle_text.Text;
  3. int found = veri.IndexOf("x");
  4. if (found != -1)
  5. {
  6.    char[] veri2 = new char[veri.IndexOf("x")];
  7.    for (int x = 0; x < (veri.IndexOf("x") + 1); x++)
  8.    {
  9.       veri.CopyTo(0, veri2, 0, x); 
  10.    }
  11. ...
the program reading the data at 30 turn per second but this speed is not enough to get the required data. so that when i change the speed to 250 turn per second (in interrupt), the data can not get synchronous catching strings.

ex: data changing like "123x", this string goes into to textbox and
1) determining "x" position on text.
2) copy the string until the position of x,
3) paste it into the an array

thanks for everything.
Sep 5 '09 #1
Share this Question
Share on Google+
7 Replies

Expert 2.5K+
P: 3,525
for loops do take a lot of time. Had you tried using the substring function?
Expand|Select|Wrap|Line Numbers
  1. string Yogi = "123x456789";
  2. string booboo = Yogi.Substring(Yogi.IndexOf('x'), Yogi.Length - (Yogi.IndexOf('x')));
  3. char[] Ranger = booboo.ToCharArray();
Or a variant on this.

Now you are executing 3 lines of code instead of several in a loop over and over.
Sep 5 '09 #2

Expert 100+
P: 1,501
Maybe you can explain the difference, but I always figured the substring function pretty much did exactly what he's doing, but made it easier for you. In terms of speed, does substring go any faster than doing the same thing in a for loop? I've always wondered, because I figured they had to be doing the same thing.
Sep 5 '09 #3

Expert 2.5K+
P: 3,525
The logic probably is similar but the for loop is actually executing multiple lines of C# language code. Each line has to be interpreted.

String.substring is written at a much lower level. It is actually part of the string type.

But I believe in proving things to one's self imperically. Start a precision timer before calling your loop 1000 times, stopping it after. Then do the same with substring can see if you can see a difference.
Sep 5 '09 #4

Expert 100+
P: 1,501
That's true I suppose... I don't actually know what the C# code is written in, or how much more efficient it is than something compiled in C#. I've been meaning to do this test but of course, once I solve my particular problem, I don't think of it anymore after that.

I'll try to remember to test it when I get up tomorrow and we'll all know once and for all!! :)
Sep 6 '09 #5

Expert 100+
P: 1,501
Well, you're right, it is faster, and significantly.

Here's the code I tested...

Expand|Select|Wrap|Line Numbers
  1.             string test = "this is a long string full of some text that I will do a subsrting on";
  3.             int loopCount = 10000;
  5.             Stopwatch sw = Stopwatch.StartNew();
  6.             for (int i = 0; i < loopCount; i++)
  7.             {
  8.                 string newStr = test.Substring(10, 10);
  9.             }
  10.             sw.Stop();
  12.             Console.WriteLine("Elapsed milliseconds for substring: " + sw.Elapsed.TotalMilliseconds);
  14.             sw.Reset();
  15.             sw.Start();
  16.             for (int i = 0; i < loopCount; i++)
  17.             {
  18.                 StringBuilder sb = new StringBuilder("");
  19.                 for (int j = 10; j < 20; j++)
  20.                 {
  21.                     sb.Append(test[j]);
  22.                 }
  23.             }
  24.             sw.Stop();
  26.             Console.WriteLine("Elapsed milliseconds for for-loop: " + sw.Elapsed.TotalMilliseconds);
I didn't even do any bounds checking, which would have slowed the for-loop down even further. Definitely interesting. If I remember, next time I'm at work (where I have the full VS suite instead of just Express like at home) I'll try making a C++ dll to see if that runs any faster ;)

Fun fun fun.
Sep 6 '09 #6

P: 2

im sorry for my late answer.. your answer very helped me, fixed problem. appreciative.
Sep 23 '09 #7

Expert 5K+
P: 7,872
I was going to comment that .CopyTo can copy a varriable amount of characters, not just one at a time.
Sep 23 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.