Improve the performance of a pointer method

Asked

Viewed 159 times

-4

Here’s the code working:

string string_pick(string text, string tag, char caractere)
{
    int index = text.IndexOf(tag);
    return text.Substring(index + tag.Length, text.IndexOf(caractere, index + tag.Length + 1) - (index + tag.Length));
}

Working example:

string x = "kkkkkkkkkkkk jjjjjjjjjjj oloco icaro alguma coisa algumas palavras várias loucuras name:'icaro' lalala huhuasd sdiufhidf sdifuhisuhdf kkkkkkk";
string temporaria = string_pick(x,"name:'",'\'');

The temporary will be "expensive".

Well, how are you gonna mess with a string gigantic, I would like to just access that part of memory and not copy the string again (in the case of what the function argument is doing).

In C++ I solved so:

string string_pick(string *text, string tag, char caractere)
{
    int index = (*text).find(tag);
    return (*text).substr(index + tag.length(), (*text).find(caractere, index + tag.length() + 1) - (index + tag.length()));
}
  • 2

    First, decide whether it is C++ or C#. The second code does not compile in either. The first seems to compile in C#. Put a [mcve]. Take advantage and explain better what the intention is.

  • Dude, the second one was an attempt, it really doesn’t work, it went wrong here, but I’m just explaining what I want to understand?

  • As for C++ or C#, it is because who knows in C++ probably would know in C# since little changes there...

  • I’ll answer what gives but if you do what I asked you I could give a better answer.

  • I’m sorry, it’s my first post on this overflow, I’ll read what you sent

1 answer

3


Before anything else do crazy things that "work" is not programming. Doing what is right is programming. Working is not a parameter to know if it is right. I will not say whether it is right or not because there is not enough information for this.

Even if it were possible to put the pointer in C# (there is even how, but in very specific situations that are not worth talking here) doing what you are trying would not solve anything to save memory since within the method is already creating a new instance. If there is any problem of memory consumption, and even this is questionable, it is within the method and not in its communication with the external world that only copies the reference and is very efficient.

If it were another problem the class StringBuilder could be a solution. But the case doesn’t even demonstrate a real performance problem or excessive allocation. And if you have the question you should demonstrate. And you should only worry about that if the problem is real and not just an assumption.

The code written in C++ is not at all more efficient than the one written in C#. Alias is a complete unnecessariness to use pointer there. He’s just creating a meaningless indirect. He’s probably crediting a quality to the pointer that he doesn’t have.

The code can be simplified and have a better nomenclature. You can start by using the name in the pattern StringPick(). The names of the parameters could indicate their function better there, not only say what will be in it, this type already says.

Just to give you an idea of what can be improved. I would do better if I knew the actual requirements.

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Why string_pick: Why I imagine I’m mining a word with a pick...(as to that is unquestionable). As for its functionality, it takes an html page and picks up a word that is inside a tag, e.g.: string_pick(htmlpage,"name:'",'''); in which case it picks up what is between Name:' and '

  • I rephrased the question and put it the way it worked in c++, take a look at

  • Thanks for the help, it’s because I saw a pointer class and out of nowhere I started imagining which pointers would slow down the process. As you asked there if it is a real problem or an assumption, I will answer, it is a real problem of a program that I made for my uncle, has a part of the process that makes a loop with that there, and on my computer that loop it takes 6 minutes, his is about 11 years older and I imagine it can take up to 2 hours to accomplish the task, so I was looking to improve the function.

  • Look at the optimization I got: (https://s31.postimg.org/ggz0ak3ff/Sem_t_tulo.png)

  • And he profiled to find out where the problem is. It doesn’t seem to be where he’s talking. Anyway, what could help this question I helped, the problem is not even clear.

  • The problem is that I was thinking of ways to make this function faster, I was wondering if it would not be better instead of copying a new string, just working on the original... But that’s not too slow, forget kk

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.