Jump to content
  • Advertisement
Sign in to follow this  
Misery

Python tuple performance

This topic is 2542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I am creating a python class which will have two one-dimensional vectors of floating point values. Lets say:


class MyClass:
def __init__(self):
self.x=()
self.y=()


Vectors x,y will be used intensively for other computations: interpolation etc.
Is python tuple a good choice? Or should I rather write my own container in C/C++ and use this one?
Would this speed up my class a lot, or it isn't worth effort?

Thanks in advance for help,
Regards

Share this post


Link to post
Share on other sites
Advertisement
Is your program running too slowly? When you run it through a profiler does it show your class to be a bottleneck? If so then you can try changing it into an extension. If not, then then you probably shouldn't be worrying about its performance.

Share this post


Link to post
Share on other sites

Is your program running too slowly? When you run it through a profiler does it show your class to be a bottleneck? If so then you can try changing it into an extension. If not, then then you probably shouldn't be worrying about its performance.


Good point!
I'm at the moment in the designing part of my program, and just made up my mind to ask if someone already tried, and could answer.
But You're absolutely right. Optimizations should be left at the end.

Thanks :]

Share this post


Link to post
Share on other sites

Hello,

I am creating a python class which will have two one-dimensional vectors of floating point values. Lets say:


class MyClass:
def __init__(self):
self.x=()
self.y=()


Vectors x,y will be used intensively for other computations: interpolation etc.
Is python tuple a good choice? Or should I rather write my own container in C/C++ and use this one?
Would this speed up my class a lot, or it isn't worth effort?

Thanks in advance for help,
Regards


Try numpy/scipy (http://numpy.scipy.org/, http://www.scipy.org/). If you need to perform interpolation on a 2D curve, you can use interp1d (http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html).

Here are some of the other things you can do with numpy/scipy: http://www.scipy.org/more_about_SciPy

The core features of numpy/scipy are written C and are considered to be quite fast. I've used them quite a bit in my day job and I'm happy with them. There should be no need for you to write your own extension.

Share this post


Link to post
Share on other sites
Thanks,
I know numpy and scipy quite well, however I decided not to use it. I decided to use a bit of polymorphism advantages.
x and y will be any iterable objects. I dont want to force anybody that will use my class to use for example numpy if it is not desired.
I am making my classes and functions so, that x,y just have to be any iterable objects. By default they are tuples.
If you want something different: just give the class constructor to

MyClassInstance.x=SomeIterableClassConstructor()


for now it works well and in case it is too slow I can always make my own C to Python array with compatible interface :]

Share this post


Link to post
Share on other sites

Thanks,
I know numpy and scipy quite well, however I decided not to use it. I decided to use a bit of polymorphism advantages.
x and y will be any iterable objects. I dont want to force anybody that will use my class to use for example numpy if it is not desired.
I am making my classes and functions so, that x,y just have to be any iterable objects. By default they are tuples.
If you want something different: just give the class constructor to

MyClassInstance.x=SomeIterableClassConstructor()


for now it works well and in case it is too slow I can always make my own C to Python array with compatible interface :]


Interesting.

Okay, dumb question: What kind of operations will you be performing on these curves constructed of arbitrary iterables? You mentioned interpolation. I'm having trouble wrapping my head around how you do interpolation on a non-numeric (but iterable) data set.

What exactly are you trying to build, if I may ask?

Share this post


Link to post
Share on other sites
Hi thok, of course You may ask :]
Maybe I don't follow Python vocabulary too well, but I am quite a noob in Python. I understand that Python list or tuple is an iterable object. So is tuple or list of floats, f.ex.:

t=(1,2,3,4)
print t[1]

And so is in my understanding numpy array, or C array with Python interface.

What I will use it for? Basic operations are:
- adding and remoing new entries
- linear interpolation between two points
- and finding the point closest to a given one

All my other functions require from used - as I call it - iterable [ ] operator and slicing.

Share this post


Link to post
Share on other sites

Hi thok, of course You may ask :]
Maybe I don't follow Python vocabulary too well, but I am quite a noob in Python. I understand that Python list or tuple is an iterable object. So is tuple or list of floats, f.ex.:

t=(1,2,3,4)
print t[1]

And so is in my understanding numpy array, or C array with Python interface.

What I will use it for? Basic operations are:
- adding and remoing new entries
- linear interpolation between two points
- and finding the point closest to a given one

All my other functions require from used - as I call it - iterable [ ] operator and slicing.


Ah, I see now. When you said 'iterable', I thought you were referring to a more general Python concept: http://docs.python.org/library/stdtypes.html#typeiter

Keep in mind that tuples are immutable; if you want to change a value or add/remove items you'll need to create a new object.

I still think you should use numpy/scipy for this. smile.png

Share this post


Link to post
Share on other sites
Well, You're right I suppose. And I can bet that in the end I will use numpy array for this. But for now I want to create a package that is somewhat standalone. I don't want User to download additional packages or libs if it is unnecessary.
We all know libs that to run or compile require other libs, and those require other libs again and so on :]
I am creating this mostly for two groups:
1) my students so they could count some hydraulic stuff in interactive session or simple script -and for them it has to be easy to use: load and forget
2) people like me - who will probably use it to create applications in it - and therefore it should be customizable

Anyway,
Thank you very much. Your answers were really great help

Share this post


Link to post
Share on other sites

Well, You're right I suppose. And I can bet that in the end I will use numpy array for this. But for now I want to create a package that is somewhat standalone. I don't want User to download additional packages or libs if it is unnecessary.
We all know libs that to run or compile require other libs, and those require other libs again and so on :]
I am creating this mostly for two groups:
1) my students so they could count some hydraulic stuff in interactive session or simple script -and for them it has to be easy to use: load and forget
2) people like me - who will probably use it to create applications in it - and therefore it should be customizable

Anyway,
Thank you very much. Your answers were really great help


Yeah, I see your point. If you have to deal with students introducing relatively large dependencies will likely cause a lot frustration (before they can actually get to the real work). If, however, everyone is running an OS with a decent package solution (i.e., most major Linux distros), installing dependencies is fast and painless.

If you're running something that isn't Linux, virtual machines can be useful (especially if you want to ensure that everyone has the exact same environment). You could basically just create a single VM image and distribute it to your students. Just throwing out some ideas. =)

Glad I could help. Cheers!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!