Skip to content

Commit

Permalink
Add StringOfMemoryAmount
Browse files Browse the repository at this point in the history
a function for printing numbers of bytes in a human-friendly format.
  • Loading branch information
stevelinton committed Oct 31, 2017
1 parent 80b7532 commit 42068cd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/ref/string.xml
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ vice versa. They are useful for examples of text encryption.
<#Include Label="NumbersString">
<#Include Label="StringNumbers">

<#Include Label="StringOfMemoryAmount">
</Section>


Expand Down
27 changes: 27 additions & 0 deletions lib/string.gd
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,33 @@ DeclareGlobalFunction("LaTeXTable");

BindGlobal("BHINT", MakeImm("\>\<"));

#############################################################################
##
#F StringOfMemoryAmount( <m> ) returns an appropriate human-readable string
## representation of <m> bytes
##
##
## <#GAPDoc Label="StringOfMemoryAmount">
## <ManSection>
## <Func Name="StringOfMemoryAmount" Arg='numbytes'/>
##
## <Description>
## This function returns a human-readable string representing
## <Arg>numbytes</Arg> of memory. It is used in printing amounts of memory
## allocated by tests and benchmarks. Binary prefixes (representing powers of
## 1024) are used.
## </Description>
## </ManSection>
## <Example><![CDATA[
## gap> StringOfMemoryAmount(123456789);
## "117MB"
## ]]></Example>
## <#/GAPDoc>


DeclareGlobalFunction("StringOfMemoryAmount");


#############################################################################
##
#E
31 changes: 31 additions & 0 deletions lib/string.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,37 @@ function(a,b)
Error("concatenating strings via + is not supported, use Concatenation(<a>,<b>) instead");
end);

#############################################################################
##
#F StringOfMemoryAmount( <m> ) returns an appropriate human-readable string
## representation of <m> bytes
##

InstallGlobalFunction(StringOfMemoryAmount, function(m)
local whole, frac, shift, s, units;
whole := m;
frac := 0;
shift := 0;
while whole >= 1024 do
frac := whole mod 1024;
whole := Int(whole / 1024);
shift := shift+1;
od;
s := ShallowCopy(String(whole));
if whole < 100 then
Append(s,".");
Append(s,String(Int(frac/102.4)));
if whole < 10 then
Append(s, String(Int(frac/10.24) mod 10));
fi;
fi;
units := ["B","KB","MB","GB","TB","PB","EB","YB","ZB"];
Append(s, units[shift+1]);
return s;
end);




#############################################################################
##
Expand Down
7 changes: 5 additions & 2 deletions lib/test.gi
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,18 @@ InstallGlobalFunction( "TestDirectory", function(arg)
totalTime := totalTime + time;

if opts.showProgress then
Print( String( time, 8 ), " msec for ", files[i].shortName, "\n" );
Print( String( time, 8 ), " msec (",String(gctime),"ms GC) and ",
StringOfMemoryAmount( mem ),
" allocated for ", files[i].shortName, "\n" );
fi;
od;

STOP_TEST := STOP_TEST_CPY;

Print("-----------------------------------\n");
Print( "total",
String( totalTime, 10 ), " msec\n\n" );
String( totalTime, 10 ), " msec (",String( totalGcTime ),"ms GC) and ",
StringOfMemoryAmount( totalMem )," allocated\n\n" );

if not opts.suppressStatusMessage then
if testTotal then
Expand Down

0 comments on commit 42068cd

Please sign in to comment.