[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology

Name
Options
Comment
Verification
4chan Pass users can bypass this verification. [Learn More] [Login]
File
  • Please read the Rules and FAQ before posting.
  • You may highlight syntax and preserve whitespace by using [code] tags.

08/21/20New boards added: /vrpg/, /vmg/, /vst/ and /vm/
05/04/17New trial board added: /bant/ - International/Random
10/04/16New board for 4chan Pass users: /vip/ - Very Important Posts
[Hide] [Show All]


[Advertise on 4chan]


File: c++ vs python.png (227 KB, 2131x1484)
227 KB
227 KB PNG
as you can see, sirs, python is providing the better simplicity and performance. task is to split a string by space and to print each word in the string. python code is 3x shorter than c++ and simple to understand. it also runs 10x faster because it does not need to compile. you simply press the run button and it runs. in conclusion, the python code is better because it works faster and is easier to understand.
>>
I just came. Yup, you read that correctly, I was masturbating to your post. Just wanted to let you know.
>>
I like the c++ one because its obvious whats happening in there. Compare the rust code you don't really know what is created/destroyed. Still it should not make much of a difference for the compiler I assume but that probably depends on what objects you are handling.
fn main() {
let text = String::from("python rules sepples drools");

let words: Vec<String> = text
.split_whitespace()
.map(|s| s.to_string())
.collect();

for word in words {
println!("{}", word);
}
}
>>
>>107643894
kek AI bot caught hook line and sinker
>>
>>107643894

shouldn’t you use Vec<&str> there instead? It’d save heap allocations iirc
>>
>>107643952
>>107643918
its from copilot no idea how rust works just wanted a example thats not c++
>>
>>107643894
>Compare the rust code you don't really know what is created/destroyed
It allocates at these function calls only:
>String::from()
>to_string()
>collect()
The point of every one of these functions is to convert to an owned type, so most Rust programmers hopefully understand that they allocate.
Rust doesn't typically do any heap allocation without an explicit function call. C++ does it all the time thanks to its constructors.
All of them are actually superfluous AI slop. You can leave them out for a more concise version that doesn't allocate:
fn main() {
let text = "python rules sepples drools";

let words = text.split_whitespace();

for word in words {
println!("{}", word);
}
}

or just:
fn main() {
let text = "python rules sepples drools";

for word in text.split_whitespace() {
println!("{}", word);
}
}

This would be the C++ equivalent (though a bit less smart about whitespace):
#include <print>
#include <string_view>
#include <ranges>

int main () {
std::string_view text = "python rules sepples drools";

for (auto word : text | std::views::split(' ')) {
std::println("{}", std::string_view(word));
}
}
>>
in haskell this is just
main = do
let text = "python rules sepples drools"
mapM_ putStrLn (words text)
>>
>>107644191

i was wondering if you could do with no allocations, thanks for showing an example, i was too tired to open docs
>>
>>107644191
>code autism
fuck security
fuck open source
fuck the new world order
>>
>>107644288
why did you click on the code autism thread
>>
>>107644261
>>107644191
That still allocates and under high memory pressure that code will fail.
>>
>>107644338
For the I/O or anywhere else?
>>
>>107643844
>used #include instead of import
>whined about verbosity but won’t import Vector, String, InputStringStream and other classes into scope
wow almost like you deliberately made c++ look unpalatable
>>
>>107644191
it is not 1999 anymore retard
import std;

using std::string_view;
using std::views::split;

int main () {
string_view text = "python rules sepples drools";

for (auto word : text | split(' ')) {
std::println("{}", string_view(word));
}
}
>>
>>107644384
I actually tried import std but couldn't find a compiler that implemented it. Can you post a godbolt link?
>>
PYTHONSAR
>>
>>107644395
Try Clang 18+ and GCC 15+. They apparently support modules.
https://arewemodulesyet.org/tools/
>>
>>107644544
Still doesn't work in Godbolt. Or locally with Clang 19:
split.cpp:1:8: fatal error: module 'std' not found
1 | import std;
| ~~~~~~~^~~
1 error generated.

Maybe more tooling would fix it but I'm not gonna install and configure a third-party build system just to use the standard library in this 10-line example program, sorry.
>>
nocoder here those arent even remotely close to being equivalent
>>
>>107644700
Obvious troll/bot thread is obvious.
>4troon retards posting code samples in a troll thread

Meanwhile, PyTroons are begging for money like street bums >>107609034



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.