php – 一种使md5_file()更快的方法?
发布时间:2020-08-16 06:30:27 所属栏目:PHP 来源:互联网
导读:我目前使用md5_file();运行大约15个URL并验证其MD5.有没有办法让我更快?经历所有这些都需要太长时间.对不起,如果这不是一个好问题,我只是意识到只有三个(四个如果你算这个)句子很久. 可能你现在依次做这个.即获取数据1,进程数据1,提取数据2,进程数据2,…和瓶
我目前使用md5_file();运行大约15个URL并验证其MD5.有没有办法让我更快?经历所有这些都需要太长时间.对不起,如果这不是一个好问题,我只是意识到只有三个(四个如果你算这个)句子很久.
可能你现在依次做这个.即获取数据1,进程数据1,提取数据2,进程数据2,…和瓶颈可能是数据传输.
编辑:使用hash extension(提供增量散列函数)和php5.3+ closure:的quick& dirty示例: $urls = array( 'https://stackoverflow.com/','http://sstatic.net/so/img/logo.png','http://www.gravatar.com/avatar/212151980ba7123c314251b185608b1d?s=128&d=identicon&r=PG','http://de.php.net/images/php.gif' ); $data = array(); $fnWrite = function($ch,$chunk) use(&$data) { foreach( $data as $d ) { if ( $ch===$d['curlrc'] ) { hash_update($d['hashrc'],$chunk); } } }; $mh = curl_multi_init(); foreach($urls as $u) { $current = curl_init(); curl_setopt($current,CURLOPT_URL,$u); curl_setopt($current,CURLOPT_RETURNTRANSFER,0); curl_setopt($current,CURLOPT_HEADER,CURLOPT_WRITEFUNCTION,$fnWrite); curl_multi_add_handle($mh,$current); $hash = hash_init('md5'); $data[] = array('url'=>$u,'curlrc'=>$current,'hashrc'=>$hash); } $active = null; //execute the handles do { $mrc = curl_multi_exec($mh,$active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh,$active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } foreach($data as $d) { curl_multi_remove_handle($mh,$d['curlrc']); echo $d['url'],': ',hash_final($d['hashrc'],false),"n"; } curl_multi_close($mh); (没有检查结果,但它只是一个起点) (编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |