435,098 Members | 1,881 Online
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 Ekle_text.Text += data; string veri = Ekle_text.Text; int found = veri.IndexOf("x"); if (found != -1) {    char[] veri2 = new char[veri.IndexOf("x")];    for (int x = 0; x < (veri.IndexOf("x") + 1); x++)    {       veri.CopyTo(0, veri2, 0, x);     } ...   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. kireytir. Sep 5 '09 #1
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 string Yogi = "123x456789"; string booboo = Yogi.Substring(Yogi.IndexOf('x'), Yogi.Length - (Yogi.IndexOf('x'))); 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             string test = "this is a long string full of some text that I will do a subsrting on";               int loopCount = 10000;               Stopwatch sw = Stopwatch.StartNew();             for (int i = 0; i < loopCount; i++)             {                 string newStr = test.Substring(10, 10);             }             sw.Stop();               Console.WriteLine("Elapsed milliseconds for substring: " + sw.Elapsed.TotalMilliseconds);               sw.Reset();             sw.Start();             for (int i = 0; i < loopCount; i++)             {                 StringBuilder sb = new StringBuilder("");                 for (int j = 10; j < 20; j++)                 {                     sb.Append(test[j]);                 }             }             sw.Stop();               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 @tlhintoq friend, 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