MD5 function efficiency in PHP

Source: Internet
Author: User
After my tests, the md5 function in PHP does increase in function time as the length of the encrypted string increases. in the practical application of PHP, do not worry about the efficiency and performance of md5 encryption with less than 100 bits
In the internal APP forum, you can see:
Because every battle requires two md5 calculations, I often use 100% of the server's cpu usage when I access more users, closed the virtual directory last night. Now we temporarily use the method of saving the calculated user's md5 to the database, however, in this way, the md5 calculation will still be performed for each battle (because the selected opponent should have never fought, and his md5 value has not been calculated before ), who can provide better methods.


Md5 function and string length
Then I studied the md5 series functions in PHP and found that some information on the Internet is actually detailed. For example, "the execution time of the md5 () function will decrease as the length of the string increases ". Although this conclusion is roughly correct, there are still some details about the demo program, because a lot of time is occupied by random strings.


After my tests, the md5 function in PHP does increase in function time as the length of the encrypted string increases. in the practical application of PHP, you do not have to worry about the efficiency and performance of md5 encryption with less than 100 bits.


Hash ('md5', 'XXX') and md5 ('XXX ')
If it is PHP, hash ('md5', 'XXX') will be 2-8 times more efficient than md5 ('XXX') when the number of characters to be encrypted is small. When the number of digits increases, for example, when there are more than 500 bits, the hash ('md5', 'XXX') and md5 ('XXX') time are almost the same, so you can use it with confidence.


Test Code
Test_hash.php
$ T1 = gettimeofday ();


For ($ I = 0; I I <10000; $ I ++ ){
$ X = hash ('md5', 'plain text about md5 efficiency in PHP, website: http://www.ccvita.com/331.html ');
}


$ T2 = gettimeofday ();


Echo ($ t2 ['SEC ']-$ t1 ['SEC']) * 1000 + ($ t2 ['usec ']-$ t1 ['usec']) /1000. "\ n ";
Test_md5.php


$ T1 = gettimeofday ();


For ($ I = 0; I I <10000; $ I ++ ){
$ X = md5 ('md5 efficiency in PHP, URL: http://www.ccvita.com/331.html ');
}


$ T2 = gettimeofday ();


Echo ($ t2 ['SEC ']-$ t1 ['SEC']) * 1000 + ($ t2 ['usec ']-$ t1 ['usec']) /1000. "\ n ";
Conclusion
1. the md5 function execution time slows down as the length of the string increases.
2. when the number of digits is small, hash ('md5', 'XXX') and md5 ('XXX') are highly efficient, but it takes a very small time to perform a single md5 operation, negligible. use the md5 function.
3. when there are many digits, for example, if there are more than 500 digits, the hash ('md5', 'XXX') and md5 ('XXX') time are almost the same, you can safely use the md5 function.
4. in general, it is advisable to directly use md5 ('XXX') to ignore hash ('md5', 'XXX.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.