-2

I want to know how the string manipulation functions are implemented internally so I can figure out their performance. Is there a way to find this out?

I'm talking about null-terminated c strings (arrays of chars) and the related functions (strcat and such) in C++, if that has anything to do with it.

Shaytan
  • 23
  • 6
  • They're probably written in assembly. – Fiddling Bits Apr 10 '15 at 14:51
  • 2
    If you're using something like GCC or Clang with libstdc++ or libc++, you could go look through the source. – chris Apr 10 '15 at 14:52
  • 2
    @chris, the C string functions aren't defined in libstdc++ or libc++ – Jonathan Wakely Apr 10 '15 at 14:52
  • 1
    Here's an interesting example [How the glibc strlen() implementation works](http://stackoverflow.com/questions/20021066/how-the-glibc-strlen-implementation-works) – Yu Hao Apr 10 '15 at 14:53
  • @FiddlingBits, they are almost certainly not. Check any of the open-source implementations for examples. – Carl Norum Apr 10 '15 at 14:53
  • 2
    Measuring their performance is much easier than trying to estimate it based on the source code. Moreover, you need to measure in your actual application, not just a benchmark or theoretical analysis -- calling patterns and cache usage of the rest of your program can have a big impact. – Ben Voigt Apr 10 '15 at 14:54
  • 1
    Just go find an open-source implementation and check it out. Newlib might be a good option, as it's a bit less heavy-duty than glibc. – Carl Norum Apr 10 '15 at 14:54
  • 1
    Here's glibc's strcat: https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strcat.c;h=6e1f1ab932ef07ef91bf5c6b8847b41b92573147;hb=HEAD – Jonathan Wakely Apr 10 '15 at 14:58
  • @JonathanWakely, Ah, right. I forgot about glibc. Thanks for pointing that out. – chris Apr 10 '15 at 15:00
  • 3
    You don't seriously think you can improve on their efficiency, do you? – molbdnilo Apr 10 '15 at 15:07
  • @molbdnilo ofc not. The purpose is to find the most efficient way to use them, not rewrite the functions which had years and years to be improved and tested. – Shaytan Apr 10 '15 at 15:13

2 Answers2

5

Is there a way to find this out?

Use the source, Luke

The sources for GNU libc string functions are easily viewable in the Git repository

You can also look at other free software or open source C libraries, such as newlib, FreeBSD, NetBSD, OpenBSD, OpenSolaris etc.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
0

if your whole purpose is to figure out the performance I don't think you need to know how it is implemented. You can feed in different data and come up with a graph and compare how the functions performed.

But if you need to study how those functions are implemented, then there is always the source code, which you can get from the internet for different C++ compilers( Not all compilers though).

DesirePRG
  • 6,122
  • 15
  • 69
  • 114