LINUX.ORG.RU

История изменений

Исправление invy, (текущая версия) :

#include <string>
#include <cstdio>
#include <ctime>
#include <cstdlib>

std::string genrandstr(const int L) {
  std::string ret(L, 0);
  for(int i = 0; i < L; i++)
    ret[i] = 32 + (rand() % 95);
  return ret;
}

int main(){
  try {
    srand(time(NULL));
    std::string s = genrandstr(rand() % 1024 + 10);

    for(int i = 0; i < 1000; ++i){
      s += genrandstr(rand() % 1014 + 10);
      printf("got: %s\n\n", s.c_str());
    }
    printf("%s\n", s.c_str());
    return 0;
  } catch (std::exception &e) {
    printf("%s\n", e.what());
    return -1;
  }
}
$ g++ test1.cpp -o test1cpp -O2 -Wall -Werror -Wextra --std=c++11
$ time for i in $(seq 1 10); do ./test1cpp > /dev/null; done

real    0m2.207s
user    0m2.184s
sys     0m0.024s
$ time for i in $(seq 1 10); do ./test1c > /dev/null; done

real    0m2.326s
user    0m2.306s
sys     0m0.022s

test1c - твой.

Исправление invy, :

#include <string>
#include <cstdio>
#include <ctime>
#include <cstdlib>

std::string genrandstr(const int L) {
  std::string ret(L, 0);
  for(int i = 0; i < L; i++)
    ret[i] = 32 + (rand() % 95);
  return ret;
}

int main(){
  try {
    srand(time(NULL));
    std::string s = genrandstr(rand() % 1024 + 10);

    for(int i = 0; i < 1000; ++i){
      s += genrandstr(rand() % 1014 + 10);
      printf("got: %s\n\n", s.c_str());
    }
    printf("%s\n", s.c_str());
    return 0;
  } catch (std::exception &e) {
    printf("%s\n", e.what());
    return -1;
  }
}
$ time for i in $(seq 1 10); do ./test1cpp > /dev/null; done

real    0m2.207s
user    0m2.184s
sys     0m0.024s
$ time for i in $(seq 1 10); do ./test1c > /dev/null; done

real    0m2.326s
user    0m2.306s
sys     0m0.022s

test1c - твой.

Исходная версия invy, :

#include <string>
#include <cstdio>
#include <ctime>
#include <cstdlib>

std::string genrandstr(const int L) {
  std::string ret(L, 0);
  for(int i = 0; i < L; i++)
    ret[i] = 32 + (rand() % 95);
  return ret;
}

int main(){
  try {
    srand(time(NULL));
    std::string s = genrandstr(rand() % 1024 + 10);

    for(int i = 0; i < 1000; ++i){
      s += genrandstr(rand() % 1014 + 10);
      printf("got: %s\n\n", s.c_str());
    }
    printf("%s\n", s.c_str());
    return 0;
  } catch (std::exception &e) {
    printf("%s\n", e.what());
    return -1;
  }
}
$ time for i in $(seq 1 10); do ./test1cpp > /dev/null; done

real    0m2.207s
user    0m2.184s
sys     0m0.024s
$ time for i in $(seq 1 10); do ./test1c > /dev/null; done

real    0m2.326s
user    0m2.306s
sys     0m0.022s