Пример #1
0
int main(int argc, char* argv[])
{
    string_t* pstr_hello = create_string();
    string_t* pstr_s = create_string();
    string_iterator_t it_pos;

    if(pstr_hello == NULL || pstr_s == NULL)
    {
        return -1;
    }

    string_init_cstr(pstr_hello, "Hello, how are you?");
    string_init_copy_range(pstr_s,
        string_begin(pstr_hello), string_end(pstr_hello));

    for(it_pos = string_begin(pstr_s);
        !iterator_equal(it_pos, string_end(pstr_s));
        it_pos = iterator_next(it_pos))
    {
        printf("%c", *(char*)iterator_get_pointer(it_pos));
    }
    printf("\n");

    algo_reverse(string_begin(pstr_s), string_end(pstr_s));
    printf("reverse: %s\n", string_c_str(pstr_s));

    algo_sort(string_begin(pstr_s), string_end(pstr_s));
    printf("ordered: %s\n", string_c_str(pstr_s));

    string_erase_range(pstr_s,
        algo_unique(string_begin(pstr_s), string_end(pstr_s)),
        string_end(pstr_s));
    printf("uniqued: %s\n", string_c_str(pstr_s));

    string_destroy(pstr_hello);
    string_destroy(pstr_s);

    return 0;
}
Пример #2
0
template <class Type> void run_with_type(
    size_t output_size, int digit, bool have_sign, bool same_size
) {
  using In = Input<Type>;

#if !defined(NDEBUG)
  std::cout << "*** DEBUG BUILD ***" << std::endl;
  // Print config before assertions
  std::cout << get_name<Type>() << " digit:" << digit << " ";
  std::cout << "sign:" << have_sign << std::endl;
#endif

  if (digit != 0) {
    if (std::pow(10, digit) > std::numeric_limits<Type>::max()) {
      std::cout << "Skip: too much digits(" << digit << ") ";
      std::cout << "for current type(" << get_name<Type>() << ")" << std::endl;
      return;
    }
  }

  if (same_size) {
    if (have_sign) {
      std::cout << "Skip: negative/positive with same size" << std::endl;
      return;
    }
    if (digit == 0) {
        std::cout << "Skip: same size with any number of digits" << std::endl;
      return;
    }
  }

  In input(output_size, digit, have_sign, same_size);
  Output output(output_size, input);

  std::cout << "Converting " << input.values().size() << " ";
  std::cout << get_name<Type>() << " with ";
  if (digit == 0) {
    std::cout << "ANY ";
  }
  else {
    std::cout << digit << " ";
  }
  std::cout << "base-10 digits ";
  if (!have_sign) {
    std::cout << "(no sign) ";
  }
  if (same_size) {
    std::cout << "(same size) ";
  }
  std::cout << "to buffer " << output.size() << " bytes" << std::endl;

  std::cout << "sizeof(short, int, long, long long, void*): ";
  std::cout << sizeof(short) << " ";
  std::cout << sizeof(int) << " ";
  std::cout << sizeof(long) << " ";
  std::cout << sizeof(long long) << " ";
  std::cout << sizeof(void*) << std::endl;

  Runner<In, AlgoFmtFormat> algo_fmt_format(input, output, "fmt::FormatInt");
  Runner<In, AlgoBoostKarma> algo_boost_karma(input, output, "boost::spirit::karma");
  Runner<In, AlgoAlexandrescu> algo_alexandrescu(input, output, "alexandrescu");
  Runner<In, AlgoReverse> algo_reverse(input, output, "reverse");
  Runner<In, AlgoTmpbuf> algo_tmpbuf(input, output, "tmpbuf");
  Runner<In, AlgoCounting> algo_counting(input, output, "counting");

  std::cout << "Run tests";

  enum {
    N = 5
  };

  for (int i = 0; i < N; ++i) {
    std::cout << " #" << i << std::flush;

    algo_fmt_format.run();
    algo_boost_karma.run();
    algo_alexandrescu.run();
    algo_reverse.run();
    algo_tmpbuf.run();
    algo_counting.run();
  }

  std::cout << "Results: " << std::endl;
  Timer::Duration algo_fmt_format_avg = algo_fmt_format.average();
  Timer::Duration algo_boost_karma_avg = algo_boost_karma.average();
  Timer::Duration algo_alexandrescu_avg = algo_alexandrescu.average();
  Timer::Duration algo_reverse_avg = algo_reverse.average();
  Timer::Duration algo_tmpbuf_avg = algo_tmpbuf.average();
  Timer::Duration algo_counting_avg = algo_counting.average();

  Timer::Duration min = std::min(
      {
      algo_fmt_format_avg,
      algo_boost_karma_avg,
      algo_alexandrescu_avg,
      algo_reverse_avg,
      algo_tmpbuf_avg,
      algo_counting_avg
      }
  );

  // Output results
  algo_fmt_format.output_result(min);
  algo_boost_karma.output_result(min);
  algo_alexandrescu.output_result(min);
  algo_reverse.output_result(min);
  algo_tmpbuf.output_result(min);
  algo_counting.output_result(min);
}