106 votes
Accepted

Load fifty million integers as quickly as possible in Java

Your code is a bit messy, and having the file name hard-coded in to your function is not great. Also, the "Hungarian Notation" (using things like arg to prefix your ...
rolfl's user avatar
  • 97.3k
56 votes

Load fifty million integers as quickly as possible in Java

Using BufferedInputStream would be a quick fix with lesser modification of current code. The reason why readX methods of ...
Aria Ax's user avatar
  • 661
21 votes
Accepted

Taking arbitrary length input in C

A simple improvement to improve performance by reducing the number of reallocs is to start with a decently sized buffer and when it fills up, grow it by a multiple of the size. For example, you could ...
mleyfman's user avatar
  • 5,170
18 votes

Load fifty million integers as quickly as possible in Java

Consider memory mapped files Disclaimer: This is also for me the first time I'm playing with memory mapped files in Java. The way I'm doing it might be suboptimal or even worse. I think that Java'...
5gon12eder's user avatar
  • 4,280
17 votes
Accepted

Read string of any length in C

/* Asks the user for string input. * Returns a pointer to the string entered by the user. * The pointer must be freed. */ Slightly misleading in that this ...
jamesdlin's user avatar
  • 296
17 votes

Brute-Force algorithm in C++

Use a better container The main issue is that std::vector<int> is a bad container for storing a set of ASCII characters. Every vector allocates on the heap, ...
G. Sliepen's user avatar
  • 55.5k
15 votes
Accepted

Emulating C++ string input in C

Don't do this: *c = realloc(*c, sizeof(**c) * (*capacity)); Once you have error handling that's more sophisticated than exit(1),...
Toby Speight's user avatar
  • 75.1k
13 votes
Accepted

Python 3 decompression routine 10x slower than C# equivalent

This kind of bit-slinging code is one of Python's weak spots, I'm afraid. But it is possible to make substantial improvements. First of all, let's establish a baseline. This is in Python 3: ...
Gareth Rees's user avatar
  • 49.6k
13 votes

Print-based debugging

Although it's a short function/macro, there are a number of problems here: __FILE__ and __LINE__ are expanded in the function ...
Toby Speight's user avatar
  • 75.1k
12 votes

Load fifty million integers as quickly as possible in Java

In the case of prime numbers your initial assumption is incorrect. Sieving is faster than reading from disk in virtually all cases. Taking primesieve as an example, generating the first 10^9 primes ...
Matthew Gauthier's user avatar
10 votes

Emulating C++ string input in C

Since @TobySpeight has already posted a wonderful answer, I'm not going to repeat what he has already posted. Just some small additional notes: ...
hoffmale's user avatar
  • 6,488
10 votes
Accepted

Network Interface Object

There is no guarantee that write writes out the entire buffer. This means that ...
vnp's user avatar
  • 56.4k
10 votes

Network Interface Object

the posted code causes the compiler to output several warning messages. When compiling, always enable the warnings, then fix those warnings. Here is the compile statement and the resulting warning ...
user3629249's user avatar
  • 2,798
10 votes

Dynamic fgets in C

Code runs into undefined behavior (UB) Code fails to test the return value of fgets(). Without knowing the return value is not ...
chux - Reinstate Monica's user avatar
10 votes

Reading 100,000 data files in Python

Avoid iterating over rows of a dataframe or array. Avoid copying data. Process one file at a time...read then write data for each file. There is no need to build a list of all the data. Perhaps use <...
RootTwo's user avatar
  • 10k
10 votes
Accepted

Python code using *args to print warnings in color

The globals aren't doing much for you, so get rid of them. Your code will not do the right thing if an argument fails to print: the colour will not be reset. Add a ...
Reinderien's user avatar
9 votes

Taking arbitrary length input in C

Functionality Error: input(stdin, 0) could quickly return NULL as code incorrectly assumes a ...
chux - Reinstate Monica's user avatar
9 votes
Accepted

Writing and reading of character files

Nice first implementation. Autoclosable / try-with-resources Please consider the AutoClosable feature from Java 8 7 to use in a try-with-resources block. See here for more detail. Split try blocks ...
Rob Audenaerde's user avatar
9 votes

Writing and reading of character files

...
Peter Taylor's user avatar
  • 24.1k
9 votes

Writing and reading of character files

Funny how nowbody seems to know the Files class... I agree with the previous answers, that try-with-resources is a good thing to do, but to perform the task in a ...
mtj's user avatar
  • 4,812
9 votes
Accepted

Brute-Force algorithm in C++

Here are a number of ideas that may help you improve your program. Don't abuse using namespace std Putting using namespace std ...
Edward's user avatar
  • 66.4k
9 votes

Input validation

Non-exported functions Declare get_valid_input and flush as static since you're in a single ...
Reinderien's user avatar
8 votes
Accepted

Recreation of cat in C

Use the C standard library Your code could be greatly simplified if you used the C standard library functions fopen, fclose, <...
5gon12eder's user avatar
  • 4,280
8 votes
Accepted

Counting newlines in a file

The two versions execute exactly the same steps, the only difference is the number of intermediary local variables. Which version is more clear can be a matter of opinion. Beginners may find your ...
janos's user avatar
  • 111k
8 votes
Accepted

Printing fizzy lines

Design: Good job on setting up everything in main(), then passing off control to another function. Declare and initialize file ...
syb0rg's user avatar
  • 21.8k
8 votes

Invoicing Program Python

What happens if the user types "s"? Your program says: Enter Buyer Name: Oops! It was supposed to be uppercase, wasn't it? I'm not sure why you want it to be ...
zondo's user avatar
  • 3,797
8 votes
Accepted

Reading strings into a vector, without using namespace std

I'll assume that you've read Why is using namespace std considered bad practice? so you understand the reasons that this can cause problems for your code. I'm not ...
Toby Speight's user avatar
  • 75.1k
8 votes

Writing and reading of character files

I'll get started on the major things first - the I/O. Stylistic ideas will come later. Try-With-Resources If you're using Java 7+, use the try-with-resources statement. The syntax is similar to C#'s ...
Tamoghna Chowdhury's user avatar
8 votes
Accepted

Delete duplicate files

To search files you're using Directory.GetFiles(), nothing wrong with that but, especially in conjunction with ...
Adriano Repetti's user avatar
8 votes
Accepted

Reading data from network stream

You should never throw an empty exception. Imagine someone uses your code and receives an empty IOException. What happened? How do you debug this? First, you need ...
IEatBagels's user avatar
  • 12.4k

Only top scored, non community-wiki answers of a minimum length are eligible