2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-04-22 12:53:47 +02:00
|
|
|
|
2024-02-03 19:14:51 +01:00
|
|
|
#include "BLI_string_ref.hh"
|
2020-04-22 12:53:47 +02:00
|
|
|
#include "BLI_timeit.hh"
|
|
|
|
|
|
2022-03-23 23:35:23 -05:00
|
|
|
#include <algorithm>
|
IO: print import & export times of Alembic & USD
Many existing importers/exporters do log the time it takes to system
console (some others log more information too). In particular, OBJ
(C++ & python), STL (C++ & python), PLY, glTF2 all log the time it
takes. However, neither USD nor Alembic do. And also it's harder to
know the time it takes there from a profiler, since all the work
normally is done on a background job and is split between several
threads (so you can't just find some top-level function and see how
much time it took).
This change:
- Adds import/export time logging to USD & Alembic importer/exporter,
- In the time utility class (also used by OBJ & STL), improve the
output formatting: 1) print only one decimal digit, 2) for long
times, print seconds and also produce a hours:minutes:seconds form.
Reviewed By: Michael Kowalski, Kévin Dietrich
Differential Revision: https://developer.blender.org/D15170
2022-07-01 12:17:50 +03:00
|
|
|
#include <iomanip>
|
2023-11-13 10:42:29 +01:00
|
|
|
#include <iostream>
|
2023-11-21 18:35:42 +01:00
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
2022-03-23 23:35:23 -05:00
|
|
|
|
2020-07-06 12:37:11 +02:00
|
|
|
namespace blender::timeit {
|
2020-04-22 12:53:47 +02:00
|
|
|
|
2023-11-21 18:35:42 +01:00
|
|
|
static void format_duration(Nanoseconds duration, fmt::memory_buffer &buf)
|
2020-04-22 12:53:47 +02:00
|
|
|
{
|
IO: print import & export times of Alembic & USD
Many existing importers/exporters do log the time it takes to system
console (some others log more information too). In particular, OBJ
(C++ & python), STL (C++ & python), PLY, glTF2 all log the time it
takes. However, neither USD nor Alembic do. And also it's harder to
know the time it takes there from a profiler, since all the work
normally is done on a background job and is split between several
threads (so you can't just find some top-level function and see how
much time it took).
This change:
- Adds import/export time logging to USD & Alembic importer/exporter,
- In the time utility class (also used by OBJ & STL), improve the
output formatting: 1) print only one decimal digit, 2) for long
times, print seconds and also produce a hours:minutes:seconds form.
Reviewed By: Michael Kowalski, Kévin Dietrich
Differential Revision: https://developer.blender.org/D15170
2022-07-01 12:17:50 +03:00
|
|
|
using namespace std::chrono;
|
|
|
|
|
if (duration < microseconds(100)) {
|
2023-11-21 18:35:42 +01:00
|
|
|
fmt::format_to(fmt::appender(buf), FMT_STRING("{} ns"), duration.count());
|
2020-04-22 12:53:47 +02:00
|
|
|
}
|
IO: print import & export times of Alembic & USD
Many existing importers/exporters do log the time it takes to system
console (some others log more information too). In particular, OBJ
(C++ & python), STL (C++ & python), PLY, glTF2 all log the time it
takes. However, neither USD nor Alembic do. And also it's harder to
know the time it takes there from a profiler, since all the work
normally is done on a background job and is split between several
threads (so you can't just find some top-level function and see how
much time it took).
This change:
- Adds import/export time logging to USD & Alembic importer/exporter,
- In the time utility class (also used by OBJ & STL), improve the
output formatting: 1) print only one decimal digit, 2) for long
times, print seconds and also produce a hours:minutes:seconds form.
Reviewed By: Michael Kowalski, Kévin Dietrich
Differential Revision: https://developer.blender.org/D15170
2022-07-01 12:17:50 +03:00
|
|
|
else if (duration < seconds(5)) {
|
2023-11-21 18:35:42 +01:00
|
|
|
fmt::format_to(fmt::appender(buf), FMT_STRING("{:.2f} ms"), duration.count() / 1.0e6);
|
IO: print import & export times of Alembic & USD
Many existing importers/exporters do log the time it takes to system
console (some others log more information too). In particular, OBJ
(C++ & python), STL (C++ & python), PLY, glTF2 all log the time it
takes. However, neither USD nor Alembic do. And also it's harder to
know the time it takes there from a profiler, since all the work
normally is done on a background job and is split between several
threads (so you can't just find some top-level function and see how
much time it took).
This change:
- Adds import/export time logging to USD & Alembic importer/exporter,
- In the time utility class (also used by OBJ & STL), improve the
output formatting: 1) print only one decimal digit, 2) for long
times, print seconds and also produce a hours:minutes:seconds form.
Reviewed By: Michael Kowalski, Kévin Dietrich
Differential Revision: https://developer.blender.org/D15170
2022-07-01 12:17:50 +03:00
|
|
|
}
|
|
|
|
|
else if (duration > seconds(90)) {
|
|
|
|
|
/* Long durations: print seconds, and also H:m:s */
|
|
|
|
|
const auto dur_hours = duration_cast<hours>(duration);
|
|
|
|
|
const auto dur_mins = duration_cast<minutes>(duration - dur_hours);
|
|
|
|
|
const auto dur_sec = duration_cast<seconds>(duration - dur_hours - dur_mins);
|
2023-11-21 18:35:42 +01:00
|
|
|
fmt::format_to(fmt::appender(buf),
|
|
|
|
|
FMT_STRING("{:.1f} s ({}H:{}m:{}s)"),
|
|
|
|
|
duration.count() / 1.0e9,
|
|
|
|
|
dur_hours.count(),
|
|
|
|
|
dur_mins.count(),
|
|
|
|
|
dur_sec.count());
|
2020-04-22 12:53:47 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2023-11-21 18:35:42 +01:00
|
|
|
fmt::format_to(fmt::appender(buf), FMT_STRING("{:.1f} s"), duration.count() / 1.0e9);
|
2020-04-22 12:53:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 18:35:42 +01:00
|
|
|
void print_duration(Nanoseconds duration)
|
|
|
|
|
{
|
|
|
|
|
fmt::memory_buffer buf;
|
|
|
|
|
format_duration(duration, buf);
|
2024-02-03 19:14:51 +01:00
|
|
|
std::cout << StringRef(buf.data(), buf.size());
|
2023-11-21 18:35:42 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-13 10:42:29 +01:00
|
|
|
ScopedTimer::~ScopedTimer()
|
|
|
|
|
{
|
|
|
|
|
const TimePoint end = Clock::now();
|
|
|
|
|
const Nanoseconds duration = end - start_;
|
|
|
|
|
|
2023-11-21 18:35:42 +01:00
|
|
|
fmt::memory_buffer buf;
|
|
|
|
|
fmt::format_to(fmt::appender(buf), FMT_STRING("Timer '{}' took "), name_);
|
|
|
|
|
format_duration(duration, buf);
|
2024-02-03 19:14:51 +01:00
|
|
|
buf.append(StringRef("\n"));
|
|
|
|
|
std::cout << StringRef(buf.data(), buf.size());
|
2023-11-13 10:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-23 23:35:23 -05:00
|
|
|
ScopedTimerAveraged::~ScopedTimerAveraged()
|
|
|
|
|
{
|
|
|
|
|
const TimePoint end = Clock::now();
|
|
|
|
|
const Nanoseconds duration = end - start_;
|
|
|
|
|
|
|
|
|
|
total_count_++;
|
|
|
|
|
total_time_ += duration;
|
|
|
|
|
min_time_ = std::min(duration, min_time_);
|
|
|
|
|
|
2023-11-21 18:35:42 +01:00
|
|
|
fmt::memory_buffer buf;
|
|
|
|
|
fmt::format_to(fmt::appender(buf), FMT_STRING("Timer '{}': (Average: "), name_);
|
|
|
|
|
format_duration(total_time_ / total_count_, buf);
|
2024-02-03 19:14:51 +01:00
|
|
|
buf.append(StringRef(", Min: "));
|
2023-11-21 18:35:42 +01:00
|
|
|
format_duration(min_time_, buf);
|
2024-02-03 19:14:51 +01:00
|
|
|
buf.append(StringRef(", Last: "));
|
2023-11-21 18:35:42 +01:00
|
|
|
format_duration(duration, buf);
|
2024-02-03 19:14:51 +01:00
|
|
|
buf.append(StringRef(")\n"));
|
|
|
|
|
std::cout << StringRef(buf.data(), buf.size());
|
2022-03-23 23:35:23 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 12:37:11 +02:00
|
|
|
} // namespace blender::timeit
|