Hot answers tagged

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 ...
  • 96.9k
66 votes
Accepted

Easier user input in C++

namespace std Others have said this already but it's important enough to be repeated: Don't put your own definitions into ...
  • 4,240
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 ...
  • 661
23 votes
Accepted

while(user == gullible)

A simple rule I (and other) try to apply is : define things in the smallest possible scope. For instance, int UserNumber = 0; can be moved inside the loop. ...
  • 29k
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 ...
  • 5,160
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'...
  • 4,240
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, ...
  • 38.2k
16 votes

"Who are you, and where do you live?" — a Q&A exercise

I have found a couple of things that could help you improve your code. Don't abuse using namespace std Putting ...
  • 64.9k
16 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 ...
  • 286
15 votes

while(user == gullible)

Given that the objective of the program is to waste time, I'm not sure that time efficiency is the thing to optimize for :P As far as anything else goes though there's room for improvement. I think ...
  • 2,216
15 votes

How can I find out why this Rust program to echo lines from stdin is slow?

I'm going to treat this as a code review question where the goal is to figure out how to write high-performance I/O code in Rust. There are two critical steps to speed up Rust I/O: Make sure the ...
  • 251
15 votes
Accepted

"ONCE", "UPON", "A", "TIME"

Problems I'm not sure that TextFileWordSplitter is a good name for the class, especially since the source of the text is, in general, a network resource rather ...
15 votes
Accepted

K&R 1-21 write an "entab" program

I see some things that could help you improve your program. Learn a recent version of C I still have my K&R first edition from 1978, however it would be a very poor choice to use for learning C ...
  • 64.9k
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),...
14 votes

"ONCE", "UPON", "A", "TIME"

In splitTextStringIntoWordList I found myself having to split textString param into String[] ...
  • 19.1k
13 votes

Parsing log files of HearthStone: The log reading API

The Design A LogReader reads a log, filters the unwanted stuff and converts it into LogEntries. According to your design. That'...
  • 22.2k
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: ...
  • 49.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 ...
12 votes

Cola Machine #1

I would build a closely-coupled mapping between the item and the message, which makes it easier to add, remove, and reorder items without things going screwy. For a really simple example: ...
  • 460
12 votes

Cola Machine #1

Yet another way of performing the dispatch according to a user selection would be with an array of strings. Or better still, with an std::array, assuming your ...
  • 17k
12 votes

Summing user input

It's good overall, and for a toy or class program this is adequate, but it's been a while since I've done a review, so it's time to get super picky :). There is a very major problem in that the IO ...
  • 10.5k
12 votes

"ONCE", "UPON", "A", "TIME"

Have you heard about Turkey? Try inserting the following line at the top of your main: ...
  • 2,919
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 ...
11 votes

"Who are you, and where do you live?" — a Q&A exercise

The first thing I notice when looking at your code is that it looks rather messy. This can easily be fixed by giving your code proper indentations. I'm not sure if the indentation is like this because ...
  • 236
11 votes
Accepted

Cola Machine #1

Good for you, avoiding using namespace std;. Within the constraints you requested (i.e. no switch block, no error handling), ...
11 votes
Accepted

Reading a line of text from a socket

I don't see anything in this code that needs to be commented. To echo what other people said, comments should clarify code and they should never say what the code already says. I would recommend ...
11 votes

Reading a line of text from a socket

Commenting the method At the minimum, public API methods should always be commented. The posted code is an implementation. You should have the method declaration, serving as the interface definition,...
  • 110k
11 votes

Parsing log files of HearthStone: The log reading API

I'll just go through this from top to bottom ...
  • 25.1k
11 votes
Accepted

Get the tail of a file (the last 10 lines)

Obviously the tail of the file is harder to do. This is a much more challenging situation than getting the first 10 lines. You have chosen a system that requires complicated byte manipulation to get ...
  • 96.9k

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