Skip to content

Ask HN: "strcmp" or "if(md5(str1) == md5(str2))"?

2 pointshagbart_celine2 comments
On HN

I need to compare to strings (hashes consisting of numbers and letters). Now i'm thingking about performance... is it better, to use strcmp or md5 both strings and compare with a if-statement?

*edit: it's in a web-app using php

Comments

Is there an md5 function that returns a primitive type?

The ones I know of return structs or arrays, which you can't compare via == in C. (I assume you're using C since strcmp is used for C strings.)

If you already have the md5's precomputed, they're probably less data, so it would be faster in that case. If you include the time to compute md5, then strcmp is definitely faster. Intuitively, strcmp just needs to do one very fast pass over the strings, with just a single comparison per character; while md5 needs to do a lot more work just to compute its output.

uhm, strcmp by far. md5 is computationally expensive and strcmp can have many optimizations.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.