ulfben icon

std::format_to_n (limit format output)

ulfben | PRO | 11/30/22 02:45:39 PM UTC (Edited) | 0 ⭐ | 1749 👁️ | Never ⏰ | []
C++ |

1.25 KB

|

None

|

0 👍

/

0 👎

void to_string(float val, std::string &out, size_t maxLength = 5) {
    out.clear();
    std::format_to_n(std::back_inserter(out), maxLength, "{:.1f}"sv, val);
}
void to_string(unsigned int val, std::string &out) {
    out.clear();
    std::format_to_n(std::back_inserter(out), 3, "{}"sv, val);
}
 
void Profiler::Profile(void) {
  _endTime = GetExactTime();
  textBox.clear();
  textBox.append(header);
  std::string ave, min, max, num; //hoisted out of loop for re-se.
  for (const auto &sample : samples) {
    assert_if_invalid(sample);
    const auto sampleTime = sample.accumulator - sample.childrensTime;
    const auto percentTime = (sampleTime / (_endTime - _startTime)) * 100.0f;
    StoreProfileInHistory(sample.name, percentTime);
    const auto [aveTime, minTime, maxTime] = GetProfileFromHistory(sample.name);
    to_string(aveTime, ave);
    to_string(minTime, min);
    to_string(maxTime, max); 
    to_string(sample.count, num);              
    textBox += std::format("{:5} : {:5} : {:5} : {:5} : {}\n"sv,
                            ave,   min,    max,   num, indent_n(sample.parentCount, sample.name));    
  }
  samples.clear();
  _startTime = GetExactTime();
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎