32 ms·
An illustration: the C++ solution takes a std::string by value, incurring an unintentional copy of the buffer. C++ makes it easy to make this mistake. C does no
by edflsafoiewq 6y ago
An illustration: the C++ solution takes a std::string by value, incurring an unintentional copy of the buffer. C++ makes it easy to make this mistake. C does not. But for the same reason C++ also makes it easy to use complicated objects and allocations when you need them for performance.
https://github.com/cesarvr/AOCRust/blob/590270ed268dcd4ff01ba8245e70e70d15e97f02/cpp/2018/day-5/main.cpp https://github.com/cesarvr/AOCRust/blob/590270ed268dcd4ff01b...
- lionkor 6y agoWow, that's some horrible code. No std::vector::reserve, string by value, memset, "using namespace std", missing includes, c-header includes in c style, ... Rarely see this kind of obscenity outside of beginner's university C++ classes.
- nyanpasu64 6y agoint hasz[200]; memset(hasz, 0, 200); yikes, that initializes the first 200 bytes to 0x00, or only the first 50 integers in the array. this code is... bad.
- felixr 6y ago> only the first 50 integers or the first 100 integers https://gcc.gnu.org/wiki/avr-gcc https://gcc.gnu.org/wiki/avr-gcc
- MertsA 6y agoFor anyone who isn't well versed in C, int is most of the time 32 bits but it's architecture dependent with some machines being e.g. 16 bit. If you need the size to be exactly 32 bits then you can just use int32_t or similar and let the compiler make that happen regardless of the architecture it's compiling to. The original code should have specified 200 * sizeof(int) or better yet since it's a local variable just ditch the memset and change it to int hasz[200] = {}; which will do the same.