-14

Let me just say right off the bat that i'm not a programmer. I'm just a guy with an idea taking his first steps to make it a reality. I'm no stranger to programming, mind you, but some of the concepts and terminology here are way over my head; so i apologize in advance if this question was answered before (i.e. Convert Python program to C/C++ code?).

I have an idea to create a simple A.I. network to analyze music data sent from a phone via cloud computing (I got a guy for the cloud stuff). It will require a lot of memory and need to be fast for the hard number-crunching. I had planned on doing it in python, but have since learned that might not be such a good idea (Is Python faster and lighter than C++?).

Since python is really the only gun i have in my holster, i was thinking of using a python-to-C++-converter. But nothing comes without a price:

  1. Is this an advantageous way to keep my code fast?
  2. What's the give-and-take for using a converter?
  3. Am i missing anything? I'm still new to this so i'm not even sure what questions to ask.

Thanks in advance.

Community
  • 1
  • 1
Noob Saibot
  • 4,573
  • 10
  • 36
  • 60
  • 9
    Think about it. If there was a Python-to-C++ converter that produced super fast code, why wouldn't that thing be in the Python interpreter/compiler? – R. Martinho Fernandes Sep 10 '12 at 01:33
  • 2
    If you want to write your application in C++ for performance reasons, learn C++ or hire someone who knows it. Otherwise, you'll have no luck with any type of converter, if such a thing even exists. – HellaMad Sep 10 '12 at 01:35
  • 2
    If you want C++... learn C++. – Ed S. Sep 10 '12 at 01:36
  • 8
    Odd juxtaposition of words: "I'm not a programmmer" and "I'm no stranger to programming"/"python is the only gun I have in my holster" – lc. Sep 10 '12 at 01:39
  • 1
    Which particular Python-to-C++ converter were you thinking about using? Or were you thinking about writing your own, without knowing any C++? – kindall Sep 10 '12 at 01:43

4 Answers4

13

Generally it's an awful way to write code, and does not guarantee that it will be any faster. Things which are simple and fast in one language can be complex and slow in another. You're better off either learning how to write fast Python code or learning C++ directly than fighting with a translator and figuring out how to make the generated code run acceptably.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

If you want C++, use C++. Note, however that PyPy have a bunch of benchmarks showing that they can be much faster than C; and with NumPy, which uses compiled extensions, numerical work becomes much faster and easier.

If you want to programme in something statically compiled, and a bit like Python, there's RPython.

Finally, you can do what NumPy does: use extensions written in C or C++ for most of your heavy computational lifting, where that appears to be appropriate, either because profiling shows a hotspot, or because you need an extension to more easily do something involving python's internals. Note that this will tie your code to a particular implementation.

Marcin
  • 48,559
  • 18
  • 128
  • 201
1

Similar to what was already stated, C++ may be faster in some areas and slower in others. Python is exactly the same. In the end, any language will be converted into machine code. It is really up to the compiler in the end to make it as efficient as it knows how to do. That said, it is better to pick one language and learn how to write fast and efficient code to do what you want.

Mlagma
  • 1,240
  • 2
  • 21
  • 49
  • Only PyPy converts it into machine code; the other implementations all use bytecode and leave the rest to the VM. – Ignacio Vazquez-Abrams Sep 10 '12 at 01:44
  • @Ignacio: ShedSkin converts into C++ and that will be compiled into machine code. Also, there's not much difference whether the VM converts it to machine code or the compiler does (well, there is in that a VM can do it better because it can profile the running process). So that's fine too. – Danny Milosavljevic Sep 16 '12 at 16:11
0

No because significant part of the good C++ performance comes from the possibility to choose the better performing architecture. It does not come magically from the same fact "because it is C".

A simple, line by line translation from Python into C++ is unlikely to increase the performance more than just using something like Cython so I think it is more reasonable to use Cython. It can still be much worse than a good developer can do with C++ from scratch. C++ simply provides more control over everything like possibility to define data type of the minimal needed length, fixed size array on stack, turn off array bounds checking in production and the like.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93