using fmt for printing

Issue #355 new
Amin Yahyaabadi created an issue

{fmt} is an open-source formatting library for C++. It can be used as a safe and fast alternative to (s)printf and iostreams.

https://github.com/fmtlib/fmt

I think if Blaze uses fmt, it can benefit from its speed and features! Fortunately, just including <fmt/ostream.h> and using fmt::print gives the benefits! This is because of fmt’s ostream support.

Doing a simple benchmark for the vector example of the wiki, most of the time is spent in printing!

// fmt::print
Excecution time: 400 microseconds
// std::cout
Excecution time: 3580 microseconds
// no printing
Excecution time: 0 microseconds
#include <iostream>
#include <chrono>

#include <fmt/ostream.h>

// macro for timing an expression
#define _time_(expression) \
  const int _time_num = 100;\
  auto _time_start = std::chrono::high_resolution_clock::now();\
  for (int i = 0; i < _time_num; ++i)\
    expression;\
  auto _time_stop = std::chrono::high_resolution_clock::now();\
  auto _time_duration = duration_cast<microseconds>(_time_stop - _time_start)/_time_num;\
  std::cout << "Excecution time: " << _time_duration.count() << " microseconds" << std::endl;\

#include <blaze/Math.h>
using namespace blaze;

int vectors() {
  StaticVector<int, 3UL> a{ 4, -2, 5 };
  DynamicVector<int> b(3UL);
  b[0] = 2;
  b[1] = 5;
  b[2] = -3;
  auto c = a + b;

  // printing using format::print
  fmt::print("c =\n{} \n", c);

  // printing using cout
  // std::cout << "c =\n" << c << "\n";

  return 0;
}

int main() {
  _time_( vectors(); );
  return 0;
}

Comments (6)

  1. Klaus Iglberger

    Hi Amin!

    Thanks a lot for creating this feature request. This is highly appreciated.

    We like the idea, but cannot simply replace the existing approach with the {fmt}. This would introduce a dependency on a non-essential third-party library. We would like to avoid that. The only option is to integrate the {fmt} for optional use. If this is something you have already done or plan to do in the near future, please feel free to provide a pull request. Thanks again,

    Best regards,

    Klaus!

  2. Log in to comment