0

I had to generate txt file with some numbers to feed to another program and this is what I made in Python:

import os

os.chdir("C:\\Users\\User\\Desktop")


if __name__ == "__main__":

    file = open("testpython.txt", "w")

    file.write("0=x 41945088 768 \n")

    d = 41945984;
    v = 896;
    offset = 1280;

    for i in range(v, 3500000000, 1408):
        file.write("".join(str(i)+"=x ")+str(d)+" "+str(offset)+"\n")
        d = d + offset + 128

    file.close()

and same in C++:

#include <fstream>

int main() {
    std::ofstream myfile;
    myfile.open("C:\\Users\\User\\Desktop\\testcpp.txt", std::ios::app);

    myfile << "0=x 41945088 768" << "\n";

    unsigned long long int d = 41945984;
    int v = 896;
    int offset = 1280;

    for (unsigned long long int i = v; i <= 3500000000; i+=offset+128) {
        
        myfile << i << "=x " << d << " " << offset << "\n";

        d = d + offset + 128;
        
    }
    
    myfile.close();
    return 0;
}

What is wrong with C++ code that it runs so long (~26s vs ~5s for Python)? I think it's quite simple and standard way of writing to file with C++? Please notice in the for loop it will be much larger number than 350000000 (I set it like that just for testing), that's why there is unsigned long long int.

Hogstrom
  • 3,581
  • 2
  • 9
  • 25
Jacob
  • 57
  • 6
  • 2
    you're not doing the same thing: in Python you overwrite (`"w"`) the file, in C++ you append to the end (`std::ios::app`). Not that I'd expect that to make a huge difference, but it at least makes the comparison questionable. – Marcus Müller Dec 18 '21 at 15:06
  • 4
    furthermore, in Python you seem to be going from 896 to 35·10⁸ in steps of 1408, in C++ in steps of 128, so 10 times as many steps. Now, the comparability is completely gone. – Marcus Müller Dec 18 '21 at 15:07
  • 1
    No idea how you compile or run the C++ version, but it runs in 400ms on my PC. – Oliver Tale-Yazdi Dec 18 '21 at 15:08
  • @MarcusMüller in C++ code there is offset+128 and offset is 1280 what makes 1408, it's the same, sorry for the confusion. – Jacob Dec 18 '21 at 15:11
  • @OliverTale-Yazdi I'm using Visual Studio 2022, local Windows debugger x64 – Jacob Dec 18 '21 at 15:13
  • please fix the appending/overwriting issue and check whether it changes your benchmarks. – Marcus Müller Dec 18 '21 at 15:16
  • 5
    @Jacob Try it with a release build, and do not run it through a Debugger. That normally slows it down. – Oliver Tale-Yazdi Dec 18 '21 at 15:17
  • yes, like, on my machine, the C++ variant takes a full 0.37 seconds to run through. The Python variant takes more than 3.3 seconds, but it loads the Python standard lib, and that's way more work then writing a couple of lines to a file, so this isn't surprising at all – Marcus Müller Dec 18 '21 at 15:19
  • 1
    Using a debugger in hot code like this is going to massively slow it down. With aggressive optimizations enabled, this takes less than half a second on my machine. – Offtkp Dec 18 '21 at 15:23
  • 1
    @OliverTale-Yazdi it helped much, ~3 sec now. However I don't know how Marcus is getting 0.37 sec. //EDIT I work on Ryzen 3200G (4c x 3.8GHz stable) – Jacob Dec 18 '21 at 15:25
  • @MarcusMüller before switching to Release mode, changing type of out operation didn't speed it up. – Jacob Dec 18 '21 at 15:26
  • Does this answer your question? [Why is Python faster than C++ in this case?](https://stackoverflow.com/questions/24895881/why-is-python-faster-than-c-in-this-case) – Shadowcoder Dec 18 '21 at 15:51
  • 1
    @Shadowcoder yup, it answers why it "was" faster, thanks! – Jacob Dec 18 '21 at 17:18

0 Answers0