[Python-Dev] C Decimal - is there any interest?

Mateusz Rukowicz mateusz.rukowicz at vp.pl
Mon Oct 15 18:00:54 CEST 2007


Hi!

    I've been working on C decimal project during gSoC 2006. After year 
of idling (I had extremely busy first year on University, but well, most 
of us are extremely busy) I decided, that I will handle further 
developing (there is still much development needed, and updating to most 
recent standard is just the beginning). I understand, that chances of 
merging C Decimal with main tree are much lower than year ago, so I 
would like to know if there is still interest in C version of Decimal. 
If so - should I write PEP, or just code and 'we'll see later'?

I've made a little benchmark - given loan amount, assets and months that 
it's meant to be payed off, find minimal monthly loan cost (It's just 
first idea that came to me for use Decimal in financial 'application' 
:>) [This solution has complexity O(log(amount) * months) which is far 
from optimal, but it's meant to benchmark Decimal, not python itself].

Code:

from _decimal import *
import sys
gc = getcontext();

def check(loan, percent, monthly):
    ret = 0
    mult = 1 + (percent / 1200)
    if (loan - monthly) * mult >= loan:
        return -1               #you cannot payoff loan ;(

    while loan > 0:
        loan = loan - monthly
        loan = loan * mult
        ret += 1
    return ret

def minimize_monthly(loan, percent, months):
    lower = Decimal(0)
    upper = Decimal(loan)

    while(upper > lower + Decimal("1e-3")):
        mid = (upper + lower)/2
        ret = check(loan, percent, mid)
        if(ret > months or ret == -1):
            lower = mid
        else:
            upper = mid


    return lower



gc.prec = int(sys.argv[4])
gc.rounding = ROUND_UP
print minimize_monthly(Decimal(sys.argv[1]), Decimal(sys.argv[2]), 
int(sys.argv[3]))

and timings (1mln loan, for 15 years, 2% year assets, and precision = 10 
:>):
mateusz at MatLaps:~/programy/python/decimal/decimal-c$ time 
../../pyth/python/python loan.py  1000000 2 180 10
6424.37955

real    0m0.068s
user    0m0.064s
sys     0m0.004s
mateusz at MatLaps:~/programy/python/decimal/decimal-c$ time 
../../pyth/python/python loan2.py  1000000 2 180 10
6424.37955

real    0m2.168s
user    0m2.148s
sys     0m0.016s

Please don't misunderstand me - I don't want to show python Decimal is 
slow, I want to show that C Decimal is worth effort. I am also aware of 
simplicity of this benchmark. (This python have been of course compiled 
with -O3).

Best regards,
Mateusz Rukowicz.


More information about the Python-Dev mailing list