note 15400 deleted from function.filesize by jimw

From: Date: Fri, 14 Dec 2001 20:17:47 +0000
Subject: note 15400 deleted from function.filesize by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-22721@lists.php.net to get a copy of this message
I don't think any of the human readable size functions given are particularly lightweight, using a loop. Here's what I would do: Function HumanSize($Size) { $Mult = Floor($Size = Log($Size) / Log(1024)); Return Round(Pow(1024, $Size - $Mult) * 100) / 100 . SubStr(" KMGT", $Mult, Min(1, $Mult)) . "B"; } Although this is not a significant case, in an interpreted language like PHP, it's important to use compiled built-in functions where ever possible. Like using the Log() function instead of a loop to calculate the logarithm of a number (that's what the loops were doing, kind of). Of course, the use of Round() in this could be improved under PHP4 to Round(..., 2). And if you are really interested in squeezing every CPU cycle, Log(1024) should be replaced by its constant value, although this form is more clear.

« previous php.notes (#22721) next »