here is the skinny (scroll down to see the problem): I am doing Huffman Encoding to compress a file using PHP (for a project). I have made the map, and made everything into a string like so:
这里有一个小问题(向下滚动查看问题):我正在执行Huffman编码,使用PHP(用于项目)压缩文件。我做了地图,把所有的东西都做成这样的一串:
00101010001100001110011101001101111011111011
Now, I need to convert that into an actual binary string, in its current state, it is only a string of 1s and 0s.
现在,我需要把它转换成一个实际的二进制字符串,在它当前的状态下,它只是一个1和0的字符串。
Here is the problem:
这是问题:
The string of 1s and 0s is 17,747,595 characters long, and it is really slowing down at around 550,000
1和0的字符串长度为17,747,595个字符,它的速度确实在55万左右。
This is the code I have:
这是我的代码:
<?php
$i=0
$len = strlen($binaryString);
while ($i < $len){
$section = substr($binaryString,$i,$i+8);
$out .= chr(bindec($section));
$i=$i+8;
}
?>
How can I make this efficient enough to run the 17 million character string?
我怎样才能使这个足够的效率来运行1700万字串呢?
Thanks very much for any support!
非常感谢您的支持!
1 个解决方案
#1
5
You don't need to loop you can use gmp
with pack
您不需要循环,您可以使用gmp与pack
$file = "binary.txt";
$string = file_get_contents($file);
$start = microtime(true);
// Convert the string
$string = simpleConvert($string);
//echo $string ;
var_dump(number_format(filesize($file),2),microtime(true)- $start);
function simpleConvert($string) {
return pack('H*',gmp_strval(gmp_init($string, 2), 16));
}
Output
输出
string '25,648,639.00' (length=13) <---- Length Grater than 17,747,595
float 1.0633520126343 <---------------- Total Conversion Time
Links
链接
- Original Dictionary File (349,900 words) 3,131KB
- 原始字典文件(349,900字)3131kb
- Binary Version 25,048 KB
- 二进制版本25048 KB
Note Solution requires GMP Functions
注意解决方案需要GMP函数
#1
5
You don't need to loop you can use gmp
with pack
您不需要循环,您可以使用gmp与pack
$file = "binary.txt";
$string = file_get_contents($file);
$start = microtime(true);
// Convert the string
$string = simpleConvert($string);
//echo $string ;
var_dump(number_format(filesize($file),2),microtime(true)- $start);
function simpleConvert($string) {
return pack('H*',gmp_strval(gmp_init($string, 2), 16));
}
Output
输出
string '25,648,639.00' (length=13) <---- Length Grater than 17,747,595
float 1.0633520126343 <---------------- Total Conversion Time
Links
链接
- Original Dictionary File (349,900 words) 3,131KB
- 原始字典文件(349,900字)3131kb
- Binary Version 25,048 KB
- 二进制版本25048 KB
Note Solution requires GMP Functions
注意解决方案需要GMP函数