MCPcopy Create free account
hub / github.com/Compaile/ctrack / measure_memory_and_calculation_time

Function measure_memory_and_calculation_time

benchmark/ctrack_benchmark.cpp:436–520  ·  view source on GitHub ↗

Measure memory usage and calculation time

Source from the content-addressed store, hash-verified

434
435// Measure memory usage and calculation time
436std::tuple<double, double, double> measure_memory_and_calculation_time()
437{
438 std::cout << "\n=== Measuring Memory Usage and Calculation Time ===" << std::endl;
439
440 // Clear any previous tracking data by getting and discarding results
441 ctrack::result_as_string();
442
443 // Measure initial memory
444 size_t initial_memory = get_memory_usage();
445
446 // Generate events
447 size_t events_per_thread = g_config.total_events / g_config.thread_count;
448
449 if (g_config.verbose)
450 {
451 std::cout << "Generating " << g_config.total_events << " events across "
452 << g_config.thread_count << " threads..." << std::endl;
453 }
454
455 auto gen_start = std::chrono::high_resolution_clock::now();
456 {
457 std::vector<std::thread> threads;
458 std::atomic<bool> start_flag{false};
459
460 for (size_t i = 0; i < g_config.thread_count; ++i)
461 {
462 threads.emplace_back(benchmark_worker, events_per_thread, std::ref(start_flag));
463 }
464
465 start_flag = true;
466
467 for (auto &t : threads)
468 {
469 t.join();
470 }
471 }
472 auto gen_end = std::chrono::high_resolution_clock::now();
473
474 // Measure memory after event generation
475 size_t post_event_memory = get_memory_usage();
476 size_t memory_used = post_event_memory - initial_memory;
477 double bytes_per_event = (double)memory_used / g_config.total_events;
478
479 if (g_config.verbose)
480 {
481 auto gen_duration = std::chrono::duration_cast<std::chrono::milliseconds>(gen_end - gen_start).count();
482 std::cout << "Event generation took: " << gen_duration << " ms" << std::endl;
483 std::cout << "Memory used: " << memory_used / (1024.0 * 1024.0) << " MB" << std::endl;
484 std::cout << "Memory per event: " << bytes_per_event << " bytes" << std::endl;
485 }
486
487 // Measure calculation time and peak memory usage
488 std::atomic<bool> monitoring{true};
489 std::atomic<size_t> peak_memory{post_event_memory};
490
491 // Start memory monitoring thread
492 std::thread monitor_thread([&monitoring, &peak_memory, initial_memory]()
493 {

Callers 1

mainFunction · 0.85

Calls 2

result_as_stringFunction · 0.85
get_memory_usageFunction · 0.85

Tested by

no test coverage detected