473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Needed Optimization for byteconversion function

Hey all,
I am writing a function that converts given bytes into KB , MB
or GB. This function will work as in Windows if the file size is too
large in MB it will show as 2.34 MB instead 23400 KB( for example )

I find still it can be optimized. Ideas welcome

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
Function : byteConversion( $byte )
Description : This functions converts given bytes into human
readable format.
Params : raw bytes
return : byte in Readable format 5.23 MB 220 KB
Version : 1.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
*/

function byteConversion( $byte ){

$result = 0;

// becoz 1 KB = 1024 Bytes
if( $byte <= 1023 ){
$result = round($byte,2)."Byte(s)";
// becoz 1 MB = 1024 KB = 1024 * 1024
}elseif( $byte <= 1048575 ){
$byte = $byte / 1024 ;
$result = round($byte,2)." KB ";
// becoz 1 GB = 1024 MB = 1024 * 1024 * 1024
}elseif( $byte <= 1073741823 ){
$byte = $byte / 1048576 ;
$result = round($byte,2)." MB ";
// becoz 1 TB = 1024 GB = 1024 * 1024 * 1024 * 1024
}elseif( $byte <= 1099511627775 ){
$byte = $byte / 1073741824 ;
$result = round($byte,2)." GB ";
}

return $result;
}

Dec 20 '05 #1
2 1428
"Prince of Code" <pr**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hey all,
I am writing a function that converts given bytes into KB , MB
or GB. This function will work as in Windows if the file size is too
large in MB it will show as 2.34 MB instead 23400 KB( for example )

I find still it can be optimized. Ideas welcome

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
Function : byteConversion( $byte )
Description : This functions converts given bytes into human
readable format.
Params : raw bytes
return : byte in Readable format 5.23 MB 220 KB
Version : 1.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
*/

function byteConversion( $byte ){

$result = 0;

// becoz 1 KB = 1024 Bytes
if( $byte <= 1023 ){
$result = round($byte,2)."Byte(s)";
// becoz 1 MB = 1024 KB = 1024 * 1024
}elseif( $byte <= 1048575 ){
$byte = $byte / 1024 ;
$result = round($byte,2)." KB ";
// becoz 1 GB = 1024 MB = 1024 * 1024 * 1024
}elseif( $byte <= 1073741823 ){
$byte = $byte / 1048576 ;
$result = round($byte,2)." MB ";
// becoz 1 TB = 1024 GB = 1024 * 1024 * 1024 * 1024
}elseif( $byte <= 1099511627775 ){
$byte = $byte / 1073741824 ;
$result = round($byte,2)." GB ";
}

return $result;
}


clearly it can be done with a loop structure:

function byteConversion($bytes){
$sizes = array('byte(s)', 'KB', 'MB', 'GB', 'TB');
for($multiples=0, $result=$bytes; $result>=1024; $multiples++,
$result/=1024);
return number_format($result,2).' '.$sizes[$multiples];
}

//examples of usage:
echo byteConversion(8192);
echo '<br/>';
echo byteConversion(1024);
echo '<br/>';
echo byteConversion(filesize(__FILE__));
echo '<br/>';
echo byteConversion(4700000000);
echo '<br/>';
echo byteConversion(1440000);
--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
an*******************@gmail.com.NOSPAM.invalid
Dec 20 '05 #2
You could, of course, just do it arithmatically:

function byteConversion($bytes) {
$units = array('byte(s)', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB',
'YB');
$unit_index = ($bytes) ? floor(log($bytes) / log(2) / 10) : 0;
$size = round($bytes / pow(2, $unit_index * 10), 2);
return "$size {$units[$unit_index]}";
}

Dec 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: spasmous | last post by:
Hi, I have an inner loop that is running too slowly. I've done my best at optimising it but am not really experienced at what works best. Can anyone suggest any optimisations? Thanks for any...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
10
by: SzH | last post by:
The code below demonstrates that the copy constructor of moo is not called on the first line of main. In spite of this, g++ (version 4.1.2) refuses to compile it if I make the copy constructor...
18
by: terminator(jam) | last post by:
consider: struct memory_pig{//a really large type: memory_pig(){ std::cout<<"mem pig default\n"; //etc... }; memory_pig(memory_pig const&){
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.