| 492 | } |
| 493 | |
| 494 | string HumanReadableElapsedTime(double seconds) { |
| 495 | string human_readable; |
| 496 | |
| 497 | if (seconds < 0) { |
| 498 | human_readable = "-"; |
| 499 | seconds = -seconds; |
| 500 | } |
| 501 | |
| 502 | // Start with us and keep going up to years. |
| 503 | // The comparisons must account for rounding to prevent the format breaking |
| 504 | // the tested condition and returning, e.g., "1e+03 us" instead of "1 ms". |
| 505 | const double microseconds = seconds * 1.0e6; |
| 506 | if (microseconds < 999.5) { |
| 507 | strings::Appendf(&human_readable, "%0.3g us", microseconds); |
| 508 | return human_readable; |
| 509 | } |
| 510 | double milliseconds = seconds * 1e3; |
| 511 | if (milliseconds >= .995 && milliseconds < 1) { |
| 512 | // Round half to even in Appendf would convert this to 0.999 ms. |
| 513 | milliseconds = 1.0; |
| 514 | } |
| 515 | if (milliseconds < 999.5) { |
| 516 | strings::Appendf(&human_readable, "%0.3g ms", milliseconds); |
| 517 | return human_readable; |
| 518 | } |
| 519 | if (seconds < 60.0) { |
| 520 | strings::Appendf(&human_readable, "%0.3g s", seconds); |
| 521 | return human_readable; |
| 522 | } |
| 523 | seconds /= 60.0; |
| 524 | if (seconds < 60.0) { |
| 525 | strings::Appendf(&human_readable, "%0.3g min", seconds); |
| 526 | return human_readable; |
| 527 | } |
| 528 | seconds /= 60.0; |
| 529 | if (seconds < 24.0) { |
| 530 | strings::Appendf(&human_readable, "%0.3g h", seconds); |
| 531 | return human_readable; |
| 532 | } |
| 533 | seconds /= 24.0; |
| 534 | if (seconds < 30.0) { |
| 535 | strings::Appendf(&human_readable, "%0.3g days", seconds); |
| 536 | return human_readable; |
| 537 | } |
| 538 | if (seconds < 365.2425) { |
| 539 | strings::Appendf(&human_readable, "%0.3g months", seconds / 30.436875); |
| 540 | return human_readable; |
| 541 | } |
| 542 | seconds /= 365.2425; |
| 543 | strings::Appendf(&human_readable, "%0.3g years", seconds); |
| 544 | return human_readable; |
| 545 | } |
| 546 | |
| 547 | } // namespace strings |
| 548 | } // namespace tensorflow |