KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to evaluate if comparing two string get slower as their length increases. My calculations suggest comparing strings should take an amortized constant time, but my Python experiments yield strange results: Here is a plot of string length (1 to 400) versus time in milliseconds. Automatic garbage collection is disabled, and gc.collect is run between every iteration. I'm comparing 1 million random strings each time, counting matches as follows.The process is repeated 50 times before taking the min of all measured times. for index in range(COUNT): if v1[index] == v2[index]: matches += 1 else: non_matches += 1 What might account for the sudden increase around length 64? Note : The following snippet can be used to try to reproduce the problem assuming v1 and v2 are two lists of random strings of length n and COUNT is their length. timeit.timeit("for i in range(COUNT): v1[i] == v2[i]", "from __main__ import COUNT, v1, v2", number=50) Further note : I've made two extra tests: comparing string with is instead of == suppresses the problem completely, and the performance is about 210ms/1M comparisons. Since interning has been mentioned, I made sure to add a white space after each string, which should prevent interning; that doesn't change anything. Is it something else than interning then?
Tags (comma-separated)
Save Edits
Cancel