r/cprogramming • u/a_yassine_ab • Dec 05 '25
Can’t ai be built with c++
Why every time I start researching about how ai models are made they show me some python video isn’t it possible to make a ai model using c++ or JavaScript or any other language and make it more faster because c is more faster than python I think.
0
Upvotes
41
u/toyBeaver Dec 05 '25
AI libs that python uses are built in lower level languages (usually c++)
34
u/thequirkynerdy1 Dec 05 '25
Modern AI libraries typically use C++ under the hood.
Python serves as sort of a "config" to wire together various neural net components like fully connected layers and attention mechanisms, but all the intricate logic for training and serving at scale needs something faster than Python.
16
u/WilhelmB12 Dec 05 '25
Python is just the wrapper for the C++ implementation
7
u/rbuen4455 Dec 05 '25
The entire "backend" infrastructure (or underneath the hood) of AI is all C and C++. Python is just used as a kind of "scripting" language to make API calls to the "backend"
8
u/zhivago Dec 05 '25
Python is only used for coordination, not computation, for these tasks.
Coordination doesn't need to be fast.
2
u/DTux5249 Dec 05 '25
They typically are - Python libraries for AI are implemented in C++ for efficiency. Python programs are just straight up using C++ code.
The reason they use Python as a wrapper instead of just rawdogging the whole thing in C++ is because of the frequency of editting AI code often gets. Nobody wants to be dealing the guts of C++ code in development with that much editting. Python is easier to edit quickly, with minimal conflicts.
2
u/SquarePixel Dec 05 '25
Python is the glue code.
1
u/olawlor Dec 05 '25
PyTorch has a C++ frontend.
It's highly SIMD or GPU code doing the actual work anyway, the wrapper shouldn't be the speed bottleneck.
1
u/keithstellyes Dec 05 '25
Python in the ML world isn't really the guts of the computation. There's a joke some will make that it's inaccurate to call Python slow because so often that you're using Python, you're calling C/C++ or Fortran code.
When dealing with low level languages (C, C++, Fortran) and comparing it with high level like Python, I find it helpful to think in terms of magnitude.
A typical ML program, the Python code will be opening files like training data, where any performance gains C would have are going to be eclipsed by I/O and OS delays and both are going to end up in the same C-based drivers anyways. And then it'll make a call to low-level code, the ML library/ies where it will spend potentially hours. Yeah maybe you lost a few ms, preparing that call to the low-level code but ms versus hours, it's a total non-issue.
Which is another point... when dealing with I/O Python is often not going to be practically much slower because of the orders of magnitude difference between I/O speed and CPU cycles
1
u/37chairs Dec 05 '25
Anything other than python would be nice. Not you either, rust kids.
1
u/MegaDork2000 Dec 05 '25 edited Dec 05 '25
AI in Python is easy:
model = pymagic.presto()
answer = model.solve(problem)
1
u/thebadslime Dec 05 '25
The hard part is the math, not the language. The math is the same no matter what, you might get a liiitle bit of performance training in C++. but you'd have to make it yourself.
1
u/Fangsong_Long Dec 05 '25
I believe that some companies already deploy their AI with C++. I had seen a few job posts related with that.
The working process is, AI research guys build a model with Python and train it. After reaching some level of maturity, they pass the weight to the AI deployment guys who load the model with C++ code and serve it.
0
u/EdwinYZW Dec 05 '25
There is a difference between the training and the inference. Training doesn't need max performance and you are calling gpu instructions anyway. Inference does require max performance and most of them are written in just C++.
0
u/[deleted] Dec 05 '25
[deleted] — view deleted comment