Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string': 0.09; 'integers': 0.09; 'key.': 0.09; 'lookup': 0.09; 'modulo': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:keys': 0.09; 'things,': 0.09; 'python': 0.11; 'adam': 0.16; 'duplicates': 0.16; 'i.e.,': 0.16; 'md5': 0.16; 'messages)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:sqlite3': 0.16; 'suppressing': 0.16; 'demonstrate': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; 'trying': 0.19; 'value.': 0.19; 'seems': 0.21; 'input': 0.22; 'select': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'headers': 0.24; 'integer': 0.24; 'tells': 0.24; '---': 0.24; 'sort': 0.25; "i've": 0.25; 'compare': 0.26; 'primary': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'testing': 0.29; "i'm": 0.30; 'equality': 0.31; 'use?': 0.31; 'text': 0.33; 'table': 0.34; "i'd": 0.34; 'could': 0.34; 'operations': 0.35; 'library.': 0.36; 'right?': 0.36; 'should': 0.36; 'so,': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'files': 0.38; 'issue': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'numbers': 0.61; 'matter': 0.61; 'such': 0.63; 'more': 0.64; 'news': 0.67; 'feeling': 0.68; 'gut': 0.84; 'premature': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: hashing strings to integers for sqlite3 keys Date: Thu, 22 May 2014 14:58:50 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9323.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400763549 news.xs4all.nl 2945 [2001:888:2000:d::a6]:49001 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71884 Adam Funk wrote: > I'm using Python 3.3 and the sqlite3 module in the standard library. > I'm processing a lot of strings from input files (among other things, > values of headers in e-mail & news messages) and suppressing > duplicates using a table of seen strings in the database. > > It seems to me --- from past experience with other things, where > testing integers for equality is faster than testing strings, as well > as from reading the SQLite3 documentation about INTEGER PRIMARY KEY > --- that the SELECT tests should be faster if I am looking up an > INTEGER PRIMARY KEY value rather than TEXT PRIMARY KEY. Is that > right? My gut feeling tells me that this would matter more for join operations than lookup of a value. If you plan to do joins you could use an autoinc integer as the primary key and an additional string key for lookup. > If so, what sort of hashing function should I use? The "maxint" for > SQLite3 is a lot smaller than the size of even MD5 hashes. The only > thing I've thought of so far is to use MD5 or SHA-something modulo the > maxint value. (Security isn't an issue --- i.e., I'm not worried > about someone trying to create a hash collision.) Start with the cheapest operation you can think of, md5(s) % MAXINT or even hash(s) % MAXINT # don't forget to set PYTHONHASHSEED then compare performance with just s and only if you can demonstrate a significant speedup keep the complication in your code. If you find such a speedup I'd like to see the numbers because this cries PREMATURE OPTIMIZATION...