Start a new topic

What does strlen do?

I've come across this interesting dilemma concerning strlen which I can't decide on because I don't have any "insider" info on how strlen actually works.

Basically, I've got two variables which can give me the length of a string if I subtract one from the other, but I'm not sure whether using this as an alternative to strlen is better for speed.


It all depends on what strlen does under the hood:

Does it count each symbol and return the sum? (which I doubt)

Or does it get the string length from some hidden variable that I can't access?



strlen does exactly what it tells to do ... it returns the plain number of characters of a given string ... nothing more, nothing less ...


I get that, that's what the documentation says, but I'm wondering HOW it calculates the number of characters in a string.

You can calculate the length of a string in different ways, right?

Maybe it counts each symbol and returns the sum?

Maybe it subtracts one address from another and returns the result?

Maybe it simply returns a value which has already been calculated and stored in some inaccessible class variable?

Maybe some other method?


If strlen is a function, then what does the function's code do?

Depending on which approach it takes, I might decide to use a different way of calculating the length of my string in order to speed things up.

sorry, but the NE-Editor is closed source, including stm firmware and tft internals ...


there are no such informations available for public ...


the most easy way for you is ...


just make a loop with a few thousend iterations ...

inside the loop just put your different strlen solutions ...

let it run and just meassure the time for each ...


Thanks for the tip!

I used the rtc5 variable in order to get the seconds and figured out the difference.


To summarize this thread, doing "strlen aString.txt,sys0" 100000 times and subtracting two variables 100000 times both take up 8 seconds, so there really isn't any difference.

with simple subtract 2 variables you won't get the exact same result than you get with substr ...

I was looking to see whether there was a substantial difference but since both take roughly 8 seconds, then it isn't that big of a deal... Besides, how can I time it in milliseconds?

...I was testing strlen and not substr. Although now that you've pointed it out, a substr operation on 6 bytes also takes roughly 8 seconds...?

Maybe my timing strategy is inaccurate?

If you want to wait ~15 minutes for an answer,

  increase loop to 1,000,000 iterations, you'll have higher precision

Nextion side - has no ms timing, so ms timing needs MCU.


get sys0

for(sys0;sys0<10000;sys0++)
{

  // loop iterations of 10000

}

get sys0


on MCU side time from

  get sys0 to get sys0, minus ((1/baud)*8) seconds


Login or Signup to post a comment