Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::cout << tab really slow? #74

Open
nickhuangxinyu opened this issue Aug 7, 2021 · 0 comments
Open

std::cout << tab really slow? #74

nickhuangxinyu opened this issue Aug 7, 2021 · 0 comments

Comments

@nickhuangxinyu
Copy link

nickhuangxinyu commented Aug 7, 2021

Hi, tabulate is very beatiful.

but compare with plain cout, it seems much slower.

my plain cout cost 0.002s but after i use tabulate, it cost 0.05 about 25 times comparing with plain.

Is there any method to speed up?

ps. i always use tabulate to show a stl map.
using table, add key row and column row, set some show style.

Did i miss anything?

thanks

ps. this is my show code:

#pragma once
#include <unordered_map>
#include <iomanip>
#include <locale>
#include <iostream>
#include <tabulate.hpp>

namespace util {
  namespace visual {
    template<class Key>
    std::vector<std::string> unfold(const std::pair<const Key, std::string>& p) {
      std::stringstream key_stream, val_stream;
      std::locale loc("C");
      key_stream.imbue(loc); val_stream.imbue(loc);
      const Key& key = p.first; const std::string& val = p.second;
      key_stream << key;
      val_stream << val;
      return {key_stream.str(), val_stream.str()};
    }

    template<class Key, class Value>
    std::vector<std::string> unfold(const std::pair<const Key, Value>& p) {
      std::stringstream key_stream, val_stream;
      std::locale loc("C");
      key_stream.imbue(loc); val_stream.imbue(loc);
      const Key& key = p.first; const Value& val = p.second;
      // printf("key = %d, val = %d\n", std::is_same<typename std::decay<Key>::type, std::string>::value, std::is_same<typename std::decay<Value>::type, std::string>::value);
      key_stream << key;
      if (val > 10 || val < -10) val_stream << (int64_t)val;
      else val_stream << std::fixed << std::setprecision(2) << val;
      return {key_stream.str(), val_stream.str()};
    }

    inline std::vector<std::string> unfold(const std::string & val) { return {val}; }

    template<typename T>
    void show(const T &m, const std::string& color="", const std::string& header = "") {
      std::cout.sync_with_stdio(false);
      std::cin.tie(nullptr);
      if (m.empty()) return;
      tabulate::Table tab;
      std::vector<std::vector<variant<std::string, const char *, tabulate::Table> > > vals;
      vals.resize(2);
      size_t count_ = 0;
      for (const auto & i : m) {
        const auto & j = unfold(i);
        size_t count = 0;
      for (const auto & i : m) {
        const auto & j = unfold(i);
        size_t count = 0;
        for (const auto& k : j) { /*printf("count=%d k=%s\n", count, k.c_str());*/ vals[count++].push_back(k); }
        count_ = count;
      }
      for (size_t i = 0 ; i < count_; ++i) tab.add_row(vals[i]);
      for (size_t i = 0; i < m.size(); ++i) {
        if (color == "red") tab[0][i].format().font_color(tabulate::Color::red).font_align(tabulate::FontAlign::center).font_style({tabulate::FontStyle::bold});
        else if (color == "green") tab[0][i].format().font_color(tabulate::Color::green).font_align(tabulate::FontAlign::center).font_style({tabulate::FontStyle::bold});
        else if (color == "yellow") tab[0][i].format().font_color(tabulate::Color::yellow).font_align(tabulate::FontAlign::center).font_style({tabulate::FontStyle::bold});
        else if (color == "blue") tab[0][i].format().font_color(tabulate::Color::blue).font_align(tabulate::FontAlign::center).font_style({tabulate::FontStyle::bold});
        else if (color == "magenta") tab[0][i].format().font_color(tabulate::Color::magenta).font_align(tabulate::FontAlign::center).font_style({tabulate::FontStyle::bold});
        else if (color == "cyan") tab[0][i].format().font_color(tabulate::Color::cyan).font_align(tabulate::FontAlign::center).font_style({tabulate::FontStyle::bold});
        else tab[0][i].format().font_align(tabulate::FontAlign::center).font_style({tabulate::FontStyle::bold});
      }
      if (vals.size() > 1) {
        auto & columns = tab.row(0);
        size_t count = 0;
        for (auto& c: columns) {
          const std::string& col = c.get_text();
          if (col.find("PNL") != std::string::npos) tab.column(count).format().width(12);
          if (col.find("MAX") != std::string::npos) tab.column(count).format().width(10);
          count++;
        }
      }
      for (size_t i = 0; i < m.size(); ++i) tab.column(i).format().font_align(tabulate::FontAlign::center);
      if (!header.empty()) std::cout << header << std::endl;
      std::cout << tab << std::endl;
    }
  }
}
 


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant