why is java md5 generation is slower than php -
i obtained md5 of string in java , php. seems java's md5 generation takes long time php's md5 generation.
my test, test.java
start = system.nanotime(); string md5 = utils.md5("sample test string"); system.out.println(md5); system.out.println((system.nanotime()-start)/1000/1000);
code utils.md5:
public static string md5(string stringtodigest) throws nosuchalgorithmexception { if(stringtodigest == null) { return ""; } messagedigest md = messagedigest.getinstance("md5"); byte[] s = md.digest(stringtodigest.getbytes()); hexbinaryadapter hba = new hexbinaryadapter(); string md5 = hba.marshal(s); return md5; }
shows me:
ccea62e4f30d422b123b9fdfb02cd496 20
test.php
<?php $start = microtime(true); echo md5('sample test string'); echo "\n".microtime(true)-$start."\n";
shows me:
ccea62e4f30d422b123b9fdfb02cd496 0.00012302398681641
so php's md5 generation taking 10-12 ms while java's taking 18-20 !! test wrong? if it's not then there way obtain md5 faster this?
edit: people complaining x slower y. have asked question because have application running , major chunk gonna redesigned in java. must serve every request within 30 ms. if md5 generation going take time, project scrapped. therefore humbly ask help. want help, respect them. want attack, please other questions
i have done tests , here explanations:
you have lot of boilerplate around generating md5. printing md5 before takes 200ms on pc, testing without it.
when replace
this
hexbinaryadapter hba = new hexbinaryadapter(); string md5 = hba.marshal(s); return md5;
with
return arrays.tostring(s);
i 18ms 6ms, lot of time taken on string conversion, because hexbinaryadapter xml related (i know arrays.tostring not covert correct string).
3.most important when take messagedigest md = messagedigest.getinstance("md5");
out of measured area (made static) 224us (microseconds). part java searches suitable provider generation of md5. so in reality not measuring generation of md5!
here code running faster:
import java.security.messagedigest; import java.security.nosuchalgorithmexception; import java.util.arrays; public class test7{ static messagedigest md; static { try { md = messagedigest.getinstance("md5"); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } } public static void main(string[] args) throws nosuchalgorithmexception { long start = system.nanotime(); string md5 = md5("sample test string"); long time = (system.nanotime()-start)/1000; system.out.println(time); system.out.println(md5); } public static string md5(string stringtodigest) throws nosuchalgorithmexception { if(stringtodigest == null) { return ""; } byte[] s = md.digest(stringtodigest.getbytes()); //hexbinaryadapter hba = new hexbinaryadapter(); return arrays.tostring(s); //string md5 = hba.marshal(s); //return md5; } }
Comments
Post a Comment