Logo ROOT   6.12/07
Reference Guide
TMemStatHelpers.cxx
Go to the documentation of this file.
1 // @(#)root/memstat:$Id$
2 // Author: Anar Manafov (A.Manafov@gsi.de) 2008-03-02
3 
4 /*************************************************************************
5 * Copyright (C) 1995-2010, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11 // STD
12 #include <iomanip>
13 #include <sstream>
14 // ROOT
15 #include "Rtypes.h"
16 // Memstat
17 #include "TMemStatHelpers.h"
18 
19 using namespace std;
20 
21 ////////////////////////////////////////////////////////////////////////////////
22 /// This function creates a string representation of the number of bytes,
23 /// represented as a number in B, kB, MB or GB depending on the value.
24 /// The result is rounded to a sensible number of digits
25 
27 {
28  ostringstream ss;
29  ss << fixed;
30 
31  if(bytes < 0) {
32  ss << '-';
33  bytes = -bytes;
34  }
35 
36  static const long kB = 1024L;
37  static const long lMB = kB * kB;
38  static const long lGB = lMB * kB;
39 
40  if(bytes < kB)
41  ss << bytes << " B";
42  else if(bytes < (10L * kB))
43  ss << setprecision(2) << ((double)bytes / (float)kB) << " kB";
44  else if(bytes < (100L * kB))
45  ss << setprecision(1) << ((double)bytes / (float)kB) << " kB";
46  else if(bytes < lMB)
47  ss << setprecision(0) << ((double)bytes / (float)kB) << " kB";
48  else if(bytes < (10L * lMB))
49  ss << setprecision(2) << ((double)bytes / (float)lMB) << " MB";
50  else if(bytes < (100L * lMB))
51  ss << setprecision(1) << ((double)bytes / (float)lMB) << " MB";
52  else if(bytes < lGB)
53  ss << setprecision(0) << ((double)bytes / (float)lMB) << " MB";
54  else
55  ss << setprecision(2) << ((double)bytes / (float)lGB) << " GB";
56 
57  return ss.str();
58 }
long long Long64_t
Definition: RtypesCore.h:69
STL namespace.
std::string dig2bytes(Long64_t bytes)
This function creates a string representation of the number of bytes, represented as a number in B...
static constexpr double L