Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //import std; funziona ma l'editor segnala errori inesistenti, scocciante
- #include <print>
- #include <chrono>
- #include <string_view>
- #include <stdexcept>
- #include <thread>
- class ExecutionTimer {
- std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
- public:
- explicit ExecutionTimer():
- start_time{ std::chrono::high_resolution_clock::now() }
- { }
- auto durata() {
- auto end_time = std::chrono::high_resolution_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
- return duration.count();
- }
- auto reset() {
- start_time = std::chrono::high_resolution_clock::now();
- }
- ~ExecutionTimer() {
- auto end_time = std::chrono::high_resolution_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
- std::println("[Timer] Tempo trascorso: {} ms", duration.count());
- }
- };
- void process_data(bool trigger_error) {
- ExecutionTimer timer{};
- // Simulazione di un'operazione che richiede tempo
- std::this_thread::sleep_for(std::chrono::milliseconds(150));
- if (trigger_error) {
- throw std::runtime_error("Errore critico durante l'elaborazione");
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- int main() {
- try {
- std::println("--- Esecuzione con successo ---");
- process_data(false);
- }
- catch (const std::exception& e) {
- std::println("Eccezione nel main: {}", e.what());
- }
- std::println("\n--- Esecuzione con errore ---");
- try {
- process_data(true);
- }
- catch (const std::exception& e) {
- std::println("Eccezione nel main: {}", e.what());
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment