I'm using python 2.5 so I don't have access to the built-in formatting.
I looked at the Django code intcomma [intcomma_recurs in code below] and realized it's inefficient, because it's recursive and also compiling the regex on every run is not a good thing either. This is not necessary an 'issue' as django isn't really THAT focused on this kind of low-level performance. Also, I was expecting a factor of 10 difference in performance, but it's only 3 times slower.
Out of curiosity I implemented a few versions of intcomma to see what the performance advantages are when using regex. My test data concludes a slight advantage for this task, but surprisingly not much at all.
I also was pleased to see what I suspected: using the reverse xrange approach is unnecessary in the no-regex case, but it does make the code look slightly better at the cost of ~10% performance.
Also, I assume what you're passing in is a string and looks somewhat like a number. Results undetermined otherwise.
from __future__ import with_statement
from contextlib import contextmanager
import re,time
re_first_num = re.compile[r"\d"]
def intcomma_noregex[value]:
end_offset, start_digit, period = len[value],re_first_num.search[value].start[],value.rfind['.']
if period == -1:
period=end_offset
segments,_from_index,leftover = [],0,[period-start_digit] % 3
for _index in xrange[start_digit+3 if not leftover else start_digit+leftover,period,3]:
segments.append[value[_from_index:_index]]
_from_index=_index
if not segments:
return value
segments.append[value[_from_index:]]
return ','.join[segments]
def intcomma_noregex_reversed[value]:
end_offset, start_digit, period = len[value],re_first_num.search[value].start[],value.rfind['.']
if period == -1:
period=end_offset
_from_index,segments = end_offset,[]
for _index in xrange[period-3,start_digit,-3]:
segments.append[value[_index:_from_index]]
_from_index=_index
if not segments:
return value
segments.append[value[:_from_index]]
return ','.join[reversed[segments]]
re_3digits = re.compile[r'[?