• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Algorithm for getting last char in string which has the most higher performance

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all.
I'm curious, what algorithm for getting last char in unicode null terminated string exists, and which one has the most performance?
I suggest only one via strlen()

Thanks!
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Do you mean a Unicode String, because a Unicode character may occupy several chars?
You can iterate the char[] until you reach the 0 and then come back 1. I suspect that is how strlen is implemented anyway.
 
Greenhorn
Posts: 1
C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I guess that Campbell Ritchie is absolutely right. And I think that this way gives better performance than strlen(). Here is comparison of this approach. Besides you don't need to include string.h.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch
(...)


The old famous C-code!

But it's been long ago since I worked with C. Does this code
also work in case of a zero length string? (because of the
++myCharArray)? I don't have a C compiler at hand, so I
can't test it.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know. I was using argc and **argv so I couldn't pass zer0‑length Strings.



Aaaaaah but I can pass zero‑length Strings. I shall try again with "" and see what happens.

The last letter of "" is 8

I can only presume that in the zero‑length String it was going back to the preceding byte in memory.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

An Stotskaya wrote: . . . Here is comparison of this approach. . . .

Now that is interesting. I shall try that and see what I get for the timings. I shall see if I can run that many times because that will give better timings than a single run.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the ++myString bit may have gone a long way ahead into memory and then gone back to find the preceding byte contains 0x38 which I believe is ASCII for the 8 character.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haven't yet managed to run that program many times, but the timings are not consistent. Sometimes using pointers is faster and sometimes using strlen.
 
reply
    Bookmark Topic Watch Topic
  • New Topic