Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

First of all, gcc gives me a warning for every occurrence of the " string" used as the parameter of runTest:

warning: conversion from string literal to 'char *' is deprecated
 [-Wdeprecated-writable-strings]

It's quite annoying when I recompile and rerun, I recommend to do it this way instead:

char* toRemove = strdup(" string");
printf("Function ran in %lu clock cycles\nProduced output: %s\n\n", runTest(rmSubstr, str1, toRemove), str1);

I was trying something similar as @William Morris @William Morris:

void rmSubstr(char *str, const char *toRemove)
{
 size_t toRemoveLength = strlen(toRemove);
 size_t origLength = strlen(str);
 char* work = str;
 int offset, remainingLength;
 while ((work = strstr(work, toRemove)))
 {
 offset = work - str;
 remainingLength = 1 + origLength - offset - toRemoveLength;
 memcpy(work, work + toRemoveLength, remainingLength);
 }
}

The idea is to move the strlen out of the loop, and calculate the remaining length using arithmetics. I don't get how strlen does it faster, but apparently it does. Even for longer test strings like this:

// after replacement, gives "oh boy"
char* str5 = strdup("oh string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string boy");

Puzzler. Note that the technique will always be slower for cases when there is nothing to remove, as in that case the strlen up front will never be used.

First of all, gcc gives me a warning for every occurrence of the " string" used as the parameter of runTest:

warning: conversion from string literal to 'char *' is deprecated
 [-Wdeprecated-writable-strings]

It's quite annoying when I recompile and rerun, I recommend to do it this way instead:

char* toRemove = strdup(" string");
printf("Function ran in %lu clock cycles\nProduced output: %s\n\n", runTest(rmSubstr, str1, toRemove), str1);

I was trying something similar as @William Morris:

void rmSubstr(char *str, const char *toRemove)
{
 size_t toRemoveLength = strlen(toRemove);
 size_t origLength = strlen(str);
 char* work = str;
 int offset, remainingLength;
 while ((work = strstr(work, toRemove)))
 {
 offset = work - str;
 remainingLength = 1 + origLength - offset - toRemoveLength;
 memcpy(work, work + toRemoveLength, remainingLength);
 }
}

The idea is to move the strlen out of the loop, and calculate the remaining length using arithmetics. I don't get how strlen does it faster, but apparently it does. Even for longer test strings like this:

// after replacement, gives "oh boy"
char* str5 = strdup("oh string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string boy");

Puzzler. Note that the technique will always be slower for cases when there is nothing to remove, as in that case the strlen up front will never be used.

First of all, gcc gives me a warning for every occurrence of the " string" used as the parameter of runTest:

warning: conversion from string literal to 'char *' is deprecated
 [-Wdeprecated-writable-strings]

It's quite annoying when I recompile and rerun, I recommend to do it this way instead:

char* toRemove = strdup(" string");
printf("Function ran in %lu clock cycles\nProduced output: %s\n\n", runTest(rmSubstr, str1, toRemove), str1);

I was trying something similar as @William Morris:

void rmSubstr(char *str, const char *toRemove)
{
 size_t toRemoveLength = strlen(toRemove);
 size_t origLength = strlen(str);
 char* work = str;
 int offset, remainingLength;
 while ((work = strstr(work, toRemove)))
 {
 offset = work - str;
 remainingLength = 1 + origLength - offset - toRemoveLength;
 memcpy(work, work + toRemoveLength, remainingLength);
 }
}

The idea is to move the strlen out of the loop, and calculate the remaining length using arithmetics. I don't get how strlen does it faster, but apparently it does. Even for longer test strings like this:

// after replacement, gives "oh boy"
char* str5 = strdup("oh string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string boy");

Puzzler. Note that the technique will always be slower for cases when there is nothing to remove, as in that case the strlen up front will never be used.

added 14 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

First of all, gcc gives me a warning for every occurrence of the " string" used as the parameter of runTest:

warning: conversion from string literal to 'char *' is deprecated
 [-Wdeprecated-writable-strings]

It's quite annoying when I recompile and rerun, I recommend to do it this way instead:

charchar* toRemove[]toRemove = strdup(" string";string");
printf("Function ran in %lu clock cycles\nProduced output: %s\n\n", runTest(rmSubstr, str1, toRemove), str1);

I was trying something similar as @William Morris:

void rmSubstr(char *str, const char *toRemove)
{
 size_t toRemoveLength = strlen(toRemove);
 size_t origLength = strlen(str);
 char* work = str;
 int offset;offset, remainingLength;
 while ((work = strstr(work, toRemove)))
 {
 offset = work - str;
 memcpy(work, work +remainingLength toRemoveLength,= 1 + origLength - offset - toRemoveLength;
  memcpy(work, work + toRemoveLength, remainingLength);
 }
}

The idea is to move the strlen out of the loop, and calculate the remaining length using arithmetics. I don't get how strlen does it faster, but apparently it does. Even for longer test strings like this:

// after replacement, gives "oh boy"
char* str5 = strdup("oh string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string boy");

Puzzler. Note that the technique will always be slower for cases when there is no matchnothing to remove, as in that case there is an extrathe strlen up front will never be used.

First of all, gcc gives me a warning for every occurrence of the " string" used as the parameter of runTest:

warning: conversion from string literal to 'char *' is deprecated
 [-Wdeprecated-writable-strings]

It's quite annoying when I recompile and rerun, I recommend to do it this way instead:

char toRemove[] = " string";
printf("Function ran in %lu clock cycles\nProduced output: %s\n\n", runTest(rmSubstr, str1, toRemove), str1);

I was trying something similar as @William Morris:

void rmSubstr(char *str, const char *toRemove)
{
 size_t toRemoveLength = strlen(toRemove);
 size_t origLength = strlen(str);
 char* work = str;
 int offset;
 while ((work = strstr(work, toRemove)))
 {
 offset = work - str;
 memcpy(work, work + toRemoveLength, 1 + origLength - offset - toRemoveLength);
 }
}

The idea is to move the strlen out of the loop, and calculate the remaining length using arithmetics. I don't get how strlen does it faster, but apparently it does. Even for longer test strings like this:

// after replacement, gives "oh boy"
char* str5 = strdup("oh string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string boy");

Puzzler. Note that the technique will always be slower for cases when there is no match, as in that case there is an extra strlen up front.

First of all, gcc gives me a warning for every occurrence of the " string" used as the parameter of runTest:

warning: conversion from string literal to 'char *' is deprecated
 [-Wdeprecated-writable-strings]

It's quite annoying when I recompile and rerun, I recommend to do it this way instead:

char* toRemove = strdup(" string");
printf("Function ran in %lu clock cycles\nProduced output: %s\n\n", runTest(rmSubstr, str1, toRemove), str1);

I was trying something similar as @William Morris:

void rmSubstr(char *str, const char *toRemove)
{
 size_t toRemoveLength = strlen(toRemove);
 size_t origLength = strlen(str);
 char* work = str;
 int offset, remainingLength;
 while ((work = strstr(work, toRemove)))
 {
 offset = work - str;
 remainingLength = 1 + origLength - offset - toRemoveLength;
  memcpy(work, work + toRemoveLength, remainingLength);
 }
}

The idea is to move the strlen out of the loop, and calculate the remaining length using arithmetics. I don't get how strlen does it faster, but apparently it does. Even for longer test strings like this:

// after replacement, gives "oh boy"
char* str5 = strdup("oh string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string boy");

Puzzler. Note that the technique will always be slower for cases when there is nothing to remove, as in that case the strlen up front will never be used.

Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

First of all, gcc gives me a warning for every occurrence of the " string" used as the parameter of runTest:

warning: conversion from string literal to 'char *' is deprecated
 [-Wdeprecated-writable-strings]

It's quite annoying when I recompile and rerun, I recommend to do it this way instead:

char toRemove[] = " string";
printf("Function ran in %lu clock cycles\nProduced output: %s\n\n", runTest(rmSubstr, str1, toRemove), str1);

I was trying something similar as @William Morris:

void rmSubstr(char *str, const char *toRemove)
{
 size_t toRemoveLength = strlen(toRemove);
 size_t origLength = strlen(str);
 char* work = str;
 int offset;
 while ((work = strstr(work, toRemove)))
 {
 offset = work - str;
 memcpy(work, work + toRemoveLength, 1 + origLength - offset - toRemoveLength);
 }
}

The idea is to move the strlen out of the loop, and calculate the remaining length using arithmetics. I don't get how strlen does it faster, but apparently it does. Even for longer test strings like this:

// after replacement, gives "oh boy"
char* str5 = strdup("oh string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string string boy");

Puzzler. Note that the technique will always be slower for cases when there is no match, as in that case there is an extra strlen up front.

lang-c

AltStyle によって変換されたページ (->オリジナル) /