0

I'm creating an image detection module, and I do a lot of math calculations around arrays.

I know that C/C++’s array iterates faster than Python’s

I can't move my project to C/C++, so I wanted to create an array module in C/C++ and call it in Python.

What I want to know:

1) Is this viable? Or calling a module from another interpreter will slow down my program more than it will speed it up?

2) Is there some Python package that does what I want?

I feel like I haven’t written enough info, but I can't think of anything else important.

[EDIT] So I just went with numpy and it has everything I need :p, thanks everyone

Alexandre Dias
  • 414
  • 4
  • 13
  • 2
    Maybe have a look at `numpy`. – juanchopanza Apr 23 '18 at 15:04
  • take a look at opencv. – UKMonkey Apr 23 '18 at 15:04
  • 1
    Yes, you *could* create your own module, but I would try Numpy first if I were you – user234461 Apr 23 '18 at 15:04
  • http://www.numpy.org/ – Ignacio Vazquez-Abrams Apr 23 '18 at 15:05
  • 5
    aside: C/C++ is not a language. – UKMonkey Apr 23 '18 at 15:05
  • Isn't Python's list type already moreorless a C array wrapped in Python? – khelwood Apr 23 '18 at 15:13
  • `I know that C/C++’s array iterates faster than Python’s` would be interested in seeing the source for this? – UKMonkey Apr 23 '18 at 15:16
  • It isnt? I assumed by how Python and C organize their memory – Alexandre Dias Apr 23 '18 at 15:17
  • @AlexandreDias an array is a contiguous chunk of memory; and iterating over it will be just starting at the start, and working along until the end. There's going to be very, very little difference between the performance for this. It's what you do with that memory that might be slower in python. – UKMonkey Apr 23 '18 at 15:19
  • @UKMonkey i mean in general the benchmarks between C++ and Python do not favor python at all: http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=python3&lang2=gpp – Grant Williams Apr 23 '18 at 15:23
  • @GrantWilliams let me repost my comment ... "It's what you do with that memory that might be slower in python" yes - python is an interpreted scripting language; it will be slower when doing a number of things. That said it's still heavily optimised; hence my question about seeing the source. – UKMonkey Apr 23 '18 at 15:27
  • 1
    @UKMonkey its still to the point where a naive loop in C++ can often beat more optimized solutions in python. You see it quite a lot when people post solutions/runtimes to problems on sites like projecteuler.net – Grant Williams Apr 23 '18 at 15:29
  • @UKMonkey I made you a tiny benchmark: https://gist.github.com/jcupitt/1a2827a020a7f958f634110e545b078f tldr: C is between 3 and 1,000 times faster than python at iterating over arrays, depending on how you code it. – jcupitt Apr 24 '18 at 10:57
  • @user894763 interesting; because from https://stackoverflow.com/questions/3917574/how-is-pythons-list-implemented a python array IS a c++ array; and the fact that you used `sum` it should fall down exactly the same code; apart from the fact that python has to interpret what you want it to do; so I'd expect "python time" = "C time" + "fixed delay to interpret" ... I might have a play with that benchmark code later :) – UKMonkey Apr 24 '18 at 13:59
  • python lists are arrays of pointers to python objects, so sum() has to follow each pointer, unpack the object, and do the add. I don't know how the py array type works but I guess it's similar. numpy is doing something very close to C and differences there are probably mostly down to compiler flags. – jcupitt Apr 24 '18 at 15:33

4 Answers4

3

Both the array and the low level operations on it would have to be in C++; switching on a per element basis will have little benefit.

There are many python modules that have internal C/C++ implementations. Simply wrapping a C or C++ style array would be pointless, as the built in python data types can basically be that.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Wait, isnt python data structed such as you cant index it? The data of an array isnt stored one value after the other in the memory, thus, making it slower than C (which does that) ?? – Alexandre Dias Apr 23 '18 at 15:16
  • @AlexandreDias There are many implementations of python. They store their data differently. See here: http://docs.python-guide.org/en/latest/starting/which-python/ -- quite possibly changing your interpreter might give you the performance you need. – Yakk - Adam Nevraumont Apr 23 '18 at 15:20
  • Thanks for the info, and I cant change the interpreter – Alexandre Dias Apr 23 '18 at 15:27
2

I think Cython might worth looking into. It can give some drastic speed improvements and is quite similar to what you are describing.

Grant Williams
  • 1,469
  • 1
  • 12
  • 24
0

It will most likely speed things up (especially if you do it for long). The only overhead will probably be at the interfacing. The c/c++ code will run however it is compiled to be ran. Though if you are just looking to wrap arrays only, you should probably take a look at other solutions first.

For interfacing python and c/c++ take a look at the post here .There's quite a few suggestions provided

Niraeth
  • 315
  • 2
  • 4
0

Try boost.python. If you can port all the computational heavy stuff to C++, it'll be quite fast but if you need to switch continuously between C++ and python, you won't get much improvement.

user8401743
  • 110
  • 8