All Questions
10,745
questions
2180
votes
10
answers
314k
views
Why is reading lines from stdin much slower in C++ than Python?
I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Python code. Since my C++ is ...
608
votes
12
answers
556k
views
Calling C/C++ from Python? [closed]
What would be the quickest way to construct a Python binding to a C or C++ library?
(I am using Windows if this matters.)
395
votes
10
answers
26k
views
How to detect a Christmas Tree?
Which image processing techniques could be used to implement an application that detects the Christmas trees displayed in the following images?
I'm searching for solutions that are going to work ...
225
votes
8
answers
564k
views
Convert Python program to C/C++ code? [closed]
is it possible to convert a Python program to C/C++?
I need to implement a couple of algorithms, and I'm not sure if the performance gap is big enough to justify all the pain I'd go through when ...
175
votes
4
answers
85k
views
TensorFlow, why was python the chosen language?
I recently started studying deep learning and other ML techniques, and I started searching for frameworks that simplify the process of build a net and training it, then I found TensorFlow, having ...
163
votes
9
answers
261k
views
How do I compile a Visual Studio project from the command-line?
I'm scripting the checkout, build, distribution, test, and commit cycle for a large C++ solution that is using Monotone, CMake, Visual Studio Express 2008, and custom tests.
All of the other parts ...
158
votes
13
answers
81k
views
Equivalent C++ to Python generator pattern
I've got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I ...
119
votes
5
answers
47k
views
Benchmarking (python vs. c++ using BLAS) and (numpy)
I would like to write a program that makes extensive use of BLAS and LAPACK linear algebra functionalities. Since performance is an issue I did some benchmarking and would like know, if the approach I ...
111
votes
6
answers
96k
views
Is there go up line character? (Opposite of \n)
I would like to overwrite something on a line above in a serial console. Is there a character that allows me to move up?
101
votes
8
answers
229k
views
Is Python faster and lighter than C++? [closed]
I've always thought that Python's advantages are code readibility and development speed, but time and memory usage were not as good as those of C++.
These stats struck me really hard.
What does your ...
100
votes
8
answers
19k
views
Why is splitting a string slower in C++ than Python?
I'm trying to convert some code from Python to C++ in an effort to gain a little bit of speed and sharpen my rusty C++ skills. Yesterday I was shocked when a naive implementation of reading lines ...
96
votes
5
answers
92k
views
Does performance differ between Python or C++ coding of OpenCV?
I aim to start opencv little by little but first I need to decide which API of OpenCV is more useful. I predict that Python implementation is shorter but running time will be more dense and slow ...
95
votes
11
answers
56k
views
Why are Python Programs often slower than the Equivalent Program Written in C or C++?
Why does Python seem slower, on average, than C/C++? I learned Python as my first programming language, but I've only just started with C and already I feel I can see a clear difference.
90
votes
10
answers
123k
views
Calling a python method from C/C++, and extracting its return value
I'd like to call a custom function that is defined in a Python module from C. I have some preliminary code to do that, but it just prints the output to stdout.
mytest.py
import math
def myabs(x):
...
88
votes
8
answers
9k
views
What's the mathematical reason behind Python choosing to round integer division toward negative infinity?
I know Python // rounds towards negative infinity and in C++ / is truncating, rounding towards 0.
And here's what I know so far:
|remainder|
-12 / 10 = -1, - 2 // C++
-12 // 10 =...
87
votes
4
answers
29k
views
c++11 regex slower than python
hi i would like to understand why the following code which does a split string split using regex
#include<regex>
#include<vector>
#include<string>
std::vector<std::string> ...
84
votes
2
answers
206k
views
How to get image width and height in OpenCV? [duplicate]
I want to get image width and height, how can I do that in OpenCV?
For example:
Mat src = imread("path_to_image");
cout << src.width;
Is that right?
84
votes
15
answers
41k
views
Is there an equivalent to the "for ... else" Python loop in C++?
Python has an interesting for statement which lets you specify an else clause.
In a construct like this one:
for i in foo:
if bar(i):
break
else:
baz()
the else clause is executed after the ...
78
votes
7
answers
15k
views
Why is Python recursion so expensive and what can we do about it?
Suppose we want to compute some Fibonacci numbers, modulo 997.
For n=500 in C++ we can run
#include <iostream>
#include <array>
std::array<int, 2> fib(unsigned n) {
if (!n)
...
78
votes
12
answers
124k
views
Integrate Python And C++
I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.
Is it possible to integrate C++ and ...
77
votes
12
answers
50k
views
Is there a compact equivalent to Python range() in C++/STL
How can I do the equivalent of the following using C++/STL? I want to fill a std::vector with a range of values [min, max).
# Python
>>> x = range(0, 10)
>>> x
[0, 1, 2, 3, 4, 5, 6, ...
76
votes
14
answers
157k
views
Py_Initialize fails - unable to load the file system codec
I am attempting to put together a simple c++ test project that uses an embedded python 3.2 interpreter. The project builds fine but Py_Initialize raises a fatal error:
Fatal Python error: ...
76
votes
7
answers
51k
views
How would you implement a basic event-loop?
If you have worked with gui toolkits, you know that there is a event-loop/main-loop that should be executed after everything is done, and that will keep the application alive and responsive to ...
74
votes
5
answers
68k
views
How to use C++ classes with ctypes?
I'm just getting started with ctypes and would like to use a C++ class that I have exported in a dll file from within python using ctypes.
So lets say my C++ code looks something like this:
class ...
73
votes
10
answers
11k
views
How are exceptions implemented under the hood? [closed]
Just about everyone uses them, but many, including me simply take it for granted that they just work.
I am looking for high-quality material. Languages I use are: Java, C, C#, Python, C++, so these ...
73
votes
9
answers
127k
views
What does "Symbol not found / Expected in: flat namespace" actually mean?
When I import a module I built, I get this boost-python related error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: dlopen(./myMod.so, 2): Symbol ...
70
votes
10
answers
34k
views
Extending python - to swig, not to swig or Cython
I found the bottleneck in my python code, played around with psycho etc. Then decided to write a c/c++ extension for performance.
With the help of swig you almost don't need to care about arguments ...
69
votes
7
answers
58k
views
ctypes error: libdc1394 error: Failed to initialize libdc1394
I'm trying to compile my program to a shared library that I can use from within Python code using ctypes.
The library compiles fine using this command:
g++ -shared -Wl,-soname,mylib -O3 -o mylib.so -...
69
votes
7
answers
128k
views
Calling python from a c++ program for distribution
I would like to call python script files from my c++ program.
I am not sure that the people I will distribute to will have python installed.
65
votes
3
answers
44k
views
cython issue: 'bool' is not a type identifier
I'm desperately trying to expose a std::vector<bool> class member to a Python class.
Here is my C++ class:
class Test
{
public:
std::vector<bool> test_fail;
std::vector<...
64
votes
10
answers
27k
views
Python: SWIG vs ctypes
In python, under what circumstances is SWIG a better choice than ctypes for calling entry points in shared libraries? Let's assume you don't already have the SWIG interface file(s). What are the ...
64
votes
12
answers
23k
views
Why do std::string operations perform poorly?
I made a test to compare string operations in several languages for choosing a language for the server-side application. The results seemed normal until I finally tried C++, which surprised me a lot. ...
62
votes
13
answers
43k
views
Is there a good Python library that can parse C++? [closed]
Google didn't turn up anything that seemed relevant.
I have a bunch of existing, working C++ code, and I'd like to use python to crawl through it and figure out relationships between classes, etc.
...
60
votes
8
answers
45k
views
Python for C++ Developers [closed]
I'm a long time C++/Java developer trying to get into Python and am looking for the stereotypical "Python for C++ Developers" article, but coming up blank. I've seen these sort of things for C#, Java,...
55
votes
5
answers
24k
views
How to get Python exception text
I want to embed python in my C++ application. I'm using Boost library - great tool. But i have one problem.
If python function throws an exception, i want to catch it and print error in my ...
55
votes
3
answers
4k
views
Why is log(inf + inf j) equal to (inf + 0.785398 j), In C++/Python/NumPy?
I've been finding a strange behaviour of log functions in C++ and numpy about the behaviour of log function handling complex infinite numbers. Specifically, log(inf + inf * 1j) equals (inf + 0.785398j)...
54
votes
6
answers
59k
views
Writing robust (color and size invariant) circle detection with OpenCV (based on Hough transform or other features)
I wrote the following very simple python code to find circles in an image:
import cv
import numpy as np
WAITKEY_DELAY_MS = 10
STOP_KEY = 'q'
cv.NamedWindow("image - press 'q' to quit", cv....
54
votes
3
answers
27k
views
Extending setuptools extension to use CMake in setup.py?
I'm writing a Python extension that links a C++ library and I'm using cmake to help with the build process. This means that right now, the only way I know how to bundle it, I have to first compile ...
52
votes
4
answers
34k
views
Compile the Python interpreter statically?
I'm building a special-purpose embedded Python interpreter and want to avoid having dependencies on dynamic libraries so I want to compile the interpreter with static libraries instead (e.g. libc.a ...
50
votes
13
answers
48k
views
Remove C and C++ comments using Python?
I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)
I realize that I could .match() substrings with a Regex, but that ...
50
votes
4
answers
45k
views
How can I use C++ class in Python?
I have implemented a class in C++. I want to use it with Python.
Please suggest step by step method and elaborate each step.
Somthing like this...
class Test{
private:
int n;
public:...
49
votes
6
answers
114k
views
Python.h: No such file or directory
I recently installed KDevelop 4 for C++ development on my Macbook Pro running Ubuntu 12.04 LTS.
I want to embed Python application in my C++ code. To do that, one needs to include the Python.h header ...
48
votes
5
answers
20k
views
Qt programming: More productive in Python or C++?
Trying to dive into Qt big time but haven't done a large project with it yet. Currently using Python, but I've been thinking -- which is really the better language to use in terms of programmer ...
48
votes
2
answers
116k
views
How to import python module from .so file?
[me@hostname python]$ cat hello_world.cc
#include <string>
#include <Python.h>
#include <boost/python.hpp>
namespace {
std::string greet() { return "Helloworld"; }
}
using ...
47
votes
7
answers
46k
views
Simple IPC between C++ and Python (cross platform)
I have a C++ process running in the background that will be generating 'events' infrequently that a Python process running on the same box will need to pick up.
The code on the C side needs to be as ...
45
votes
4
answers
44k
views
Sending a C++ array to Python and back (Extending C++ with Numpy)
I am going to send a c++ array to a python function as numpy array and get back another numpy array. After consulting with numpy documentation and some other threads and tweaking the code, finally the ...
44
votes
7
answers
45k
views
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
I am building a C++ extension for use in Python. I am seeing this warning being generated during the compilation process - when a type:
python setup.py build_ext -i
What is causing it, and how do I ...
44
votes
3
answers
21k
views
Has anyone used Sphinx to document a C++ project? [closed]
Sphinx is a new documentation tool for Python. It looks very nice. What I'm wondering is:
How suitable this is for documenting a C++ project?
Are there any tools for converting existing documentation ...
43
votes
2
answers
21k
views
C++ GDB Python Pretty Printing Tutorial?
I am looking for a solid tutorial on creating a custom printer.
There are a few sparse pages on the GDB page however they are far from complete and leave much to the imagination!
For instance, I'd ...
42
votes
3
answers
34k
views
returning numpy arrays via pybind11
I have a C++ function computing a large tensor which I would like to return to Python as a NumPy array via pybind11.
From the documentation of pybind11, it seems like using STL unique_ptr is ...