Calculate the time complexity of the algorithm!
Take a look at the following code:
public string GetMD5Hash(string input) { System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bs = System.Text.Encoding.UTF8.GetBytes(input); bs = x.ComputeHash(bs); System.Text.StringBuilder s = new System.Text.StringBuilder(); foreach (byte b in bs) { s.Append(b.ToString("x2").ToLower()); } string password = s.ToString(); return password; }
If we calculated the time complexity, we would get T = 11 + n * 2, but it’s just “what we see”, that is, ToLower can do the hard work that we don’t know. But from now on, we see that this algorithm is O (n) in all cases. The value of time grows as data grows.
Also, in order to address the cache problem, I would prefer to have “hard” work in memory, since memory is cheaper compared to using a CPU.
Filip Ekberg
source share