The fastest way to change a string to C

Is this function the fastest, most optimized way to access a string in C? This runs in O (n / 2) time. The optimization is that it only iterates through half the line.

char* str_reverse(char *str, int len) { char word[len]; int i; for (i = 0; i <= len / 2; ++i) { word[i] = str[len - i - 1]; word[len - i - 1] = str[i]; } word[len] = '\0'; return word; } 
+7
c
source share
6 answers

Maybe something like this?

 char *str_reverse_in_place(char *str, int len) { char *p1 = str; char *p2 = str + len - 1; while (p1 < p2) { char tmp = *p1; *p1++ = *p2; *p2-- = tmp; } return str; } 
+10
source share

You will find algorithms that take fewer instructions, for example, in place of reverse

 char* str_reverse_in_place(char *str, int len) { int i; for (i = len/2-1 ; i >= 0 ; --i) { char c = str[i]; str[i] = str[len-i-1]; str[len-i-1] = c; } return str; } 

Speed ​​optimization at this level, look at the inline keyword, also compile with (for gcc) using -O3 (usually this is better than adding a register ... yourself).

If you need an inverted string elsewhere, either specify it in a function (it is assigned to strlen(str)+1 - actually len+1 here are characters)

 char* str_reverse(char *str, char *reverse, int len) { int i; for (i = len-1 ; i >= 0 ; --i) { reverse[i] = str[len-i-1]; } reverse[len] = 0; return reverse; } 

or malloc it (it must be freed by the caller).

 char* str_reverse_malloc(char *str, int len) { char *reverse = malloc(len+1); if ( ! reverse) return NULL; int i; for (i = len-1 ; i >= 0 ; --i) { reverse[i] = str[len-i-1]; } reverse[len] = 0; return reverse; } 
+3
source share

The optimized “most” way should solve the issue of processor architecture and memory, as well as what is reversed (long lines or short lines, and what is the distribution).

It is not possible to restore the O (N) requirement, but methods such as loop reversal, loop locking, and parallelism can be used to optimize cache misses for very large lines. You can also increase the size of the word and the word swap, dwords, or larger objects in place (dealing with a possible alignment problem).

// This is likely to be faster than byte copying, but it is not O (N / 8) ...

 if (len & 7 == 0) { uint32_t *dst = src+len-4; uint32_t *src = (uint32_t *)ptr; while (src<dst) { a = *src; b = *dst; *src++ = byte_swap(b); *dst-- = byte_swap(a); } } 
+3
source share
 int main() { char str[100], temp; int i, j = 0; printf("\nEnter the string :"); gets(str); i = 0; j = strlen(str) - 1; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } printf("\nReverse string is :%s", str); return (0); } 
+2
source share

Here is an option that does not require length skipping and will replace both the start and end characters with a given offset within the string, each of which goes through a loop:

 /** strrevstr - reverse string, swaps src & dest each iteration. * Takes valid string and reverses, original is not preserved. * If str is valid, returns pointer to str, NULL otherwise. */ char *strrevstr (char *str) { if (!str) { printf ("strrevstr() error: invalid string\n"); return NULL; } char *begin = str; char *end = str + strlen (str) - 1; char tmp; while (end > begin) { tmp = *end; *end-- = *begin; *begin++ = tmp; } return str; } 
0
source share

This is the fastest way to print the return line in c

 #include <stdio.h> #include <string.h> int main() { char str[30],str1[30]; printf("Enter string :"); scanf("%s",str); int i = strlen(str); for (int j = 0; j <=5;j++) { str1[j]=str[ij]; printf("%c",str1[j]); } printf("\n"); } 
-one
source share

All Articles