Skip to main content
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.4k
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
  • 60.1k
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
  • 79.3k
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
  • 79.3k
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,508
10 votes
Accepted

Network Interface Object

There is no guarantee that write writes out the entire buffer. This means that ...
vnp's user avatar
  • 57k
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,818
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
  • 63.1k
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,822
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.5k
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
  • 63.1k
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
  • 79.3k
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
8 votes

Accepting user input for tickets and tips

There's no such thing as an "If" loop, but perhaps you meant a While loop? That is exactly what you need. Otherwise this looks like a pretty good start. Next you might consider: adding some error ...
TheAtomicOption's user avatar
8 votes
Accepted

Accepting user input for tickets and tips

To expand on TheAtomicOption's answer, we should first try and perform some validation so that the values in your program are as expected. Starting with this section: ...
Phrancis's user avatar
  • 20.4k
8 votes

Accepting user input for tickets and tips

Others already provided great answers, but I'd like to add something about portability. I see you used the python2.7 tag, but I feel like (at least for this snippet of code) there shouldn't be a ...
Daniel's user avatar
  • 4,500
8 votes
Accepted

Implementing an alternative to scanf

Q: How can the algorithm of the function be improved? A: I'm not sure what you mean by algorithm. There are several things relating to functionality that you should improve on, however: Make an enum ...
Ben Steffan's user avatar
  • 5,228
8 votes

Dynamic fgets in C

Make up your mind. dynamic_fgets returns the same information via return value (return *rtr) and the in-out parameter (...
vnp's user avatar
  • 57k

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