473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

find character in string speed (synchronous) problem

kireytir
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. ...
  12.  
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 3048
tlhintoq
3,525 Expert 2GB
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
GaryTexmo
1,501 Expert 1GB
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
tlhintoq
3,525 Expert 2GB
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
GaryTexmo
1,501 Expert 1GB
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
GaryTexmo
1,501 Expert 1GB
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";
  2.  
  3.             int loopCount = 10000;
  4.  
  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();
  11.  
  12.             Console.WriteLine("Elapsed milliseconds for substring: " + sw.Elapsed.TotalMilliseconds);
  13.  
  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();
  25.  
  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
@tlhintoq
friend,

im sorry for my late answer.. your answer very helped me, fixed problem. appreciative.
Sep 23 '09 #7
Plater
7,872 Expert 4TB
I was going to comment that .CopyTo can copy a varriable amount of characters, not just one at a time.
Sep 23 '09 #8

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

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
11
by: johnny | last post by:
Can anyone tell me what is wrong with the following code fragment? char a = x; char b = y; int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10); I got the same result using...
6
by: MLH | last post by:
SELECT Adresses.RawData FROM Adresses WHERE (((Adresses.RawData)=InStr(1,,Chr$(13)))); Its not doing the trick for me and I'm not sure why.
22
by: Annajiat | last post by:
Hi, a) int grid; Which of the following is faster? 1. memset(grid,0,1010*1010); 2. memset(grid,0,sizeof(grid)); b) Is there any faster alternative to initializing memory? c) What is the...
14
by: inpuarg | last post by:
I want to find a & character using Regex. But not && How can i manage this in c# Quickfind window ? -------------------------------------------------- ne kadar yaşarsan yaşa sevdiğin kadardır...
4
by: Dameon | last post by:
Hi All, I have a process where I'd like to search the contents of a file(in a dir) for all occurences (or the count of) of a given string. My goal is to focus more on performance, as some of the...
6
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have...
4
by: Leon | last post by:
Hi everybody, I am a beginer for Python, hope can get help from you guys. What I want to do is : Input an ID -find the ID in the file -copy the whole string <str id='xxx'>yyyyy</str> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.