Fastest way to find the length of string in c


 
Thread Tools Search this Thread
Top Forums Programming Fastest way to find the length of string in c
# 1  
Old 06-15-2012
Fastest way to find the length of string in c

Hi all,
We use strlen() fun provided by library to find the length of a string.
Looking inside of it, it has some different mechanism to find the length of string.
Normally, we scan the string byte by byte until the '\0' character. It takes a logn time to count length.

The Library strlen() function writer scan 4 bytes at a time and checks whether the 4 byte has
'\0' character as below.

Code:
#include <string.h>
#include <stdlib.h>

#undef strlen

/* Return the length of the null-terminated string STR.  Scan for
   the null terminator quickly by testing four bytes at a time.  */
size_t
strlen (str)
     const char *str;
{
  const char *char_ptr;
  const unsigned long int *longword_ptr;
  unsigned long int longword, himagic, lomagic;

  /* Handle the first few characters by reading one character at a time.
     Do this until CHAR_PTR is aligned on a longword boundary.  */
  for (char_ptr = str; ((unsigned long int) char_ptr
			& (sizeof (longword) - 1)) != 0;
       ++char_ptr)
    if (*char_ptr == '\0')
      return char_ptr - str;

  /* All these elucidatory comments refer to 4-byte longwords,
     but the theory applies equally well to 8-byte longwords.  */

  longword_ptr = (unsigned long int *) char_ptr;

  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
     the "holes."  Note that there is a hole just to the left of
     each byte, with an extra at the end:

     bits:  01111110 11111110 11111110 11111111
     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD

     The 1-bits make sure that carries propagate to the next 0-bit.
     The 0-bits provide holes for carries to fall into.  */
  himagic = 0x80808080L;
  lomagic = 0x01010101L;
  if (sizeof (longword) > 4)
    {
      /* 64-bit version of the magic.  */
      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
      himagic = ((himagic << 16) << 16) | himagic;
      lomagic = ((lomagic << 16) << 16) | lomagic;
    }
  if (sizeof (longword) > 8)
    abort ();

  /* Instead of the traditional loop which tests each character,
     we will test a longword at a time.  The tricky part is testing
     if *any of the four* bytes in the longword in question are zero.  */
  for (;\;\) ///   '\'  is not the part of code, it is escape character.
    {
      longword = *longword_ptr++;

      if (((longword - lomagic) & ~longword & himagic) != 0)
	{
	  /* Which of the bytes was the zero?  If none of them were, it was
	     a misfire; continue the search.  */

	  const char *cp = (const char *) (longword_ptr - 1);

	  if (cp[0] == 0)
	    return cp - str;
	  if (cp[1] == 0)
	    return cp - str + 1;
	  if (cp[2] == 0)
	    return cp - str + 2;
	  if (cp[3] == 0)
	    return cp - str + 3;
	  if (sizeof (longword) > 4)
	    {
	      if (cp[4] == 0)
		return cp - str + 4;
	      if (cp[5] == 0)
		return cp - str + 5;
	      if (cp[6] == 0)
		return cp - str + 6;
	      if (cp[7] == 0)
		return cp - str + 7;
	    }
	}
    }
}
libc_hidden_builtin_def (strlen)


DOES ANY BODY EXPLAIN THE WORKING OF CODE..
Moderator's Comments:
Mod Comment please use code tags

Last edited by bakunin; 06-15-2012 at 11:27 AM.. Reason: corrected tags
# 2  
Old 06-15-2012
I don't think, your approach will gain any speed. Most implementation of strlen use assembler code to maximize speed. Some CPU architectures can do strlen in a single assembler instruction. You can't top that with whatever C code you might come up with.

For reference: in x86 assembler, the relevant instruction is repne scasb

BTW: strlen and other simple string functions are ofter compiled as inline without calling a library function.
# 3  
Old 06-15-2012
Quote:
Normally, we scan the string byte by byte until the '\0' character. It takes a logn time to count length.
How is that log(n)?

I see no benefit to your code.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String Length

Hi All, One of my source file is having Date column and the format of the column is YYYY-MM-DD. As per my business logic I have to check if the date format either YYY-MM-DD or YYYY-M-DD. If any records are in this format then I have print all the records and send those invalid records through... (4 Replies)
Discussion started by: suresh_target
4 Replies

2. Shell Programming and Scripting

Help to find length of string avoiding trailing spaces

Hi, I have a record of length 200 bytes and values filled is only 100 bytes and remaining 100 spaces is occupied by spaces. In script wen i try to find the length of the entire record it should get as 200 not 100. i tried using length and wc -c but it doesnt work can anyone have any idea on... (3 Replies)
Discussion started by: Pranaveen
3 Replies

3. Programming

How to find length of string and pass into char array in C?

Hi All I want to take a Hexadecimal number as input and i want to find lenth of the input and pass it to char s ( char s ). I have a program to convert hexadecial to binary but it is taking limited input but i want to return binary number based on input. How? (1 Reply)
Discussion started by: atharalikhan
1 Replies

4. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

5. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

6. Shell Programming and Scripting

find the string length in solaris

how to find the string length in solaris machine. (4 Replies)
Discussion started by: din_annauniv
4 Replies

7. Shell Programming and Scripting

Awk:Find length of string omitting quotes

Hi , I have a file named "sample" having the data as follows. "663005487","USD",0,1,"NR" If i give like a=`awk -F ',' '{printf length($2)}' sample` (Trying to find length of second field)I should get the output for the above as 3 (Omitting double quotes) not 5. How to do this..... (2 Replies)
Discussion started by: jayakumarrt
2 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question