[IronPython] Sin and Sqrt performance (Chris Trimble)

Dino Viehland dinov at exchange.microsoft.com
Tue Apr 18 00:52:25 CEST 2006


I believe most of what you're experiencing is the cost of being dynamic, although we certainly have a lot of room for improvement here...

I looked at this under the profiler and there is some sillyness in IronPython that can yield a ~10% perf gain on this micro benchmark (for example in place add of floating point doubles checks for 3 other types before seeing if the other type is a double - most likely it's a double and we should check that first).

There's also a problem in that you hit an extremely slow code path in IronPython (in place addition).  This is currently slow so we can get the correct semantics (of trying one type, then trying another w/o throwing after the 1st type failed).  This is something we can improve, but it's not a small work item - I've gone ahead and filed a bug on this.  Changing the code to do j = j + xyz yields about a 25% perf improvement for me though.


Also, it looks like the range() call takes ~10% of the time on IronPython (the actual creation of the range) so I'm a little surprised that doesn't yield improvements either.

We also probably take an additional perf hit because when we're doing the optimized dispatch to call sin and sqrt we don't do an inline type-check for double first.  Instead we go down a code path that involves a call (Converter.TryConvertToDouble) which we could avoid...

Which brings me back to the cost of being dynamic.  All of these small improvements (not that 35% is something to look down at) won't get us that close to C#'s performance.  What would be better here for the long-term is adding type inference support to IronPython so we can do this all very fast, and fall back to the slow path if things change due to being dynamic.  This is something that we've considered doing, but it's not a 1.0 feature.  Even then I think we'd be looking good if we got within 2x of C#'s performance given all the extra goodies you get by switching to a dynamic langauge.

That being said, if at the end of the day some critical piece of code is running slow you can always fall back to C# to write that small piece of code.  You can then load from IronPython and call into it directly.  All in all the interop experience here is pretty smooth so this shouldn't create many problems (other than the logistics of having 2 programming languages, of course).


Do you want to help develop Dynamic languages on CLR? (http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038)

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Chris Trimble
Sent: Monday, April 17, 2006 2:59 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Sin and Sqrt performance (Chris Trimble)

I shouldn't have said "no difference".  Should have written... xrange
or while does actually make around a 10% difference.  Of course this
is nowhere near the 500x performance increase that C# seems to offer,
which is why I said "no difference". :)

Thanks,

 - C


On 4/17/06, Chris Trimble <trimble at pobox.com> wrote:
> Did you try making the change on your end and see a change?  It makes
> no difference on my end.
>
> BTW, I had tried 'while' before making the original post.  No
> difference there either.  In fact, range() has better performance than
> 'while' in CPython for this code!
>
>   - Chris
>
>
>
>
> On 4/17/06, Dave <midnightdf at yahoo.com> wrote:
> > Use xrange instead of range and there should be a pretty good performance
> > improvement;) As is, your for loop creates a list of five million elements.
> > A simple while loop would also do the trick although it's not as clean to
> > look at.
> >
> > Dave
> >
> > users-request at lists.ironpython.com wrote:
> >  Send users mailing list submissions to
> >  users at lists.ironpython.com
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> > or, via email, send a message with subject or body 'help' to
> >  users-request at lists.ironpython.com
> >
> > You can reach the person managing the list at
> >  users-owner at lists.ironpython.com
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of users digest..."
> >
> >
> > Today's Topics:
> >
> >  1. Sin and Sqrt performance (Chris Trimble)
> >
> >
> > ----------------------------------------------------------------------
> >
> > Message: 1
> > Date: Mon, 17 Apr 2006 11:47:35 -0700
> > From: "Chris Trimble"
> > Subject: [IronPython] Sin and Sqrt performance
> > To: "Discussion of IronPython"
> > Message-ID:
> >
> > Content-Type: text/plain; charset=ISO-8859-1
> >
> > IP doesn't do much better than CPython when using math.sin and
> > math.sqrt. However, same test in C# dominates (10ms.. as opposed to
> > upwards of 5 seconds in IP or CPy). Am I missing something here?
> >
> > Thanks,
> >
> >  - Chris
> >
> > -----------------------------------
> >
> > import time
> > from math import *
> >
> > def do_timing(f, reps):
> >  start = time.time()
> >  f(reps)
> >  end = time.time()
> >  print "%s (%d reps): %f" % (f.__name__, reps, end-start)
> >
> >
> > def py_fpfunc_test(reps):
> >  j = 0.0
> >  for i in range(0, reps):
> >  j += sin( j )
> >  j += sqrt( j )
> >  j +=2.72392032032;
> >  print j
> >
> > def do_all_timing():
> >  do_timing(py_fpfunc_test, 5000000)
> >
> > do_all_timing()
> > print ""
> >
> >
> > ------------------------------
> >
> > _______________________________________________
> > users mailing list
> > users at lists.ironpython.com
> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >
> >
> > End of users Digest, Vol 21, Issue 18
> > *************************************
> >
> >
> >
> >  ________________________________
> > How low will we go? Check out Yahoo! Messenger's low PC-to-Phone call rates.
> >
> >
> > _______________________________________________
> > users mailing list
> > users at lists.ironpython.com
> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >
> >
> >
>
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list