mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
optimize profiler.cpp
This commit is contained in:
parent
af7c1afe9c
commit
e88f57ae30
@ -11,9 +11,10 @@ struct LineRecord{
|
|||||||
i64 hits;
|
i64 hits;
|
||||||
clock_t time;
|
clock_t time;
|
||||||
|
|
||||||
LineRecord(int line, SourceData* src): line(line), src(src), hits(0), time(0) {}
|
LineRecord(): line(-1), src(nullptr), hits(0), time(0) {}
|
||||||
|
|
||||||
std::string_view line_content() const;
|
std::string_view line_content() const;
|
||||||
|
bool is_valid() const { return src != nullptr; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LineProfiler{
|
struct LineProfiler{
|
||||||
@ -22,7 +23,7 @@ struct LineProfiler{
|
|||||||
int prev_line;
|
int prev_line;
|
||||||
|
|
||||||
// filename -> records
|
// filename -> records
|
||||||
std::map<std::string_view, std::map<int, LineRecord>> records;
|
std::map<std::string_view, std::vector<LineRecord>> records;
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
void _step(Frame* frame);
|
void _step(Frame* frame);
|
||||||
|
@ -37,9 +37,14 @@ void LineProfiler::_step(Frame *frame){
|
|||||||
_step_end();
|
_step_end();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<int, LineRecord>& file_records = records[filename];
|
std::vector<LineRecord>& file_records = records[filename];
|
||||||
auto [it, ok] = file_records.insert({line, LineRecord(line, frame->co->src.get())});
|
if(file_records.empty()) file_records.resize(frame->co->src->line_starts.size() + 10);
|
||||||
prev_record = &(it->second);
|
|
||||||
|
prev_record = &file_records[line];
|
||||||
|
if(!prev_record->is_valid()){
|
||||||
|
prev_record->line = line;
|
||||||
|
prev_record->src = frame->co->src.get();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineProfiler::_step_end(){
|
void LineProfiler::_step_end(){
|
||||||
@ -61,15 +66,17 @@ Str LineProfiler::stats(){
|
|||||||
SStream ss;
|
SStream ss;
|
||||||
for(auto& [filename, file_records] : records){
|
for(auto& [filename, file_records] : records){
|
||||||
clock_t total_time = 0;
|
clock_t total_time = 0;
|
||||||
for(auto& [line, record] : file_records){
|
for(auto& record: file_records){
|
||||||
total_time += record.time;
|
if(record.is_valid()) total_time += record.time;
|
||||||
}
|
}
|
||||||
ss << "Total time: " << (f64)total_time / CLOCKS_PER_SEC << "s\n";
|
ss << "Total time: " << (f64)total_time / CLOCKS_PER_SEC << "s\n";
|
||||||
ss << "File: " << filename << "\n";
|
ss << "File: " << filename << "\n";
|
||||||
// ss << "Function: " << "<?>" << "at line " << -1 << "\n";
|
// ss << "Function: " << "<?>" << "at line " << -1 << "\n";
|
||||||
ss << "Line # Hits Time Per Hit % Time Line Contents\n";
|
ss << "Line # Hits Time Per Hit % Time Line Contents\n";
|
||||||
ss << "==============================================================\n";
|
ss << "==============================================================\n";
|
||||||
for(auto& [line, record]: file_records){
|
for(int line = 1; line < file_records.size(); line++){
|
||||||
|
LineRecord& record = file_records[line];
|
||||||
|
if(!record.is_valid()) continue;
|
||||||
ss << left_pad(std::to_string(line), 6);
|
ss << left_pad(std::to_string(line), 6);
|
||||||
ss << left_pad(std::to_string(record.hits), 10);
|
ss << left_pad(std::to_string(record.hits), 10);
|
||||||
ss << left_pad(std::to_string(record.time), 13);
|
ss << left_pad(std::to_string(record.time), 13);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user