28 votes

Extending Sieve of Eratosthenes beyond a billion

A few things you can do: Use a bit vector (with bit manipulation) instead of a bool array. This gives you 8 times more memory. If you use C++ vector< bool> ...
Andreas H.'s user avatar
24 votes
Accepted

Intersection over Union for rotated rectangles

1. Review The large bounty suggests that getting a review of this code is important. But there are some other things you could have done to make reviewing this code more productive: Give us some ...
Gareth Rees's user avatar
  • 49.6k
20 votes

Printing 300 pages

I try to manually dispose as much as I can This is usually good however this time you dispose too much. ...
t3chb0t's user avatar
  • 44.1k
17 votes
Accepted

Extending Sieve of Eratosthenes beyond a billion

There is a huge number of ways to make this run faster. First you save an awful lot of space by storing only odd numbers in the sieve. The only even prime is the number 2. Note that on a modern ...
gnasher729's user avatar
  • 2,654
15 votes
Accepted

Bruteforce MD5 Password cracker

First let's mention a bug / issue: Your passwords variable is accessed (read and modified) from multiple goroutines without any syncronization: this is a race ...
icza's user avatar
  • 946
14 votes
Accepted

Printing 300 pages

don't mix german and english words for names. Best is to stick to english because most/all developers knows the language. don't repeat yourself. You have some duplicated code which should be removed. ...
Heslacher's user avatar
  • 50.3k
14 votes

Read CSV and use bidirectional BFS to find the shortest connections between actors

It looks like you found a pretty nice approach there already. A few general things stand out: Using globals Magical constants and unsafe C-style arrays/raw pointers: ...
sehe's user avatar
  • 1,322
12 votes

Binary file validity in C++/C++11: checking a binary header

The most unforgivable fault with this function is that calling isValidBuffer() also erases the buffer! That completely violates the Principle of Least Surprise. If ...
200_success's user avatar
12 votes
Accepted

Where it all begins: Khronos

Without more of the code, it's not really going to be possible to address memory management or bottlenecks. So instead, I'll look instead at readability and portability. Use the appropriate ...
Edward's user avatar
  • 65.3k
12 votes

Number guess game program in python 3

Your comments describe what the code is doing, not why the code's doing what it is. And so are generally unhelpful. Please don't use profanity in comments, or anywhere. It makes you look ...
Peilonrayz's user avatar
  • 42.7k
11 votes

Printing 300 pages

Do not manually call Dispose(), you will end-up having some duplication (and you're not even disposing those resources in case of errors...). Use ...
Adriano Repetti's user avatar
10 votes

Rust Brainfuck interpreter

Profile You can only improve what you can measure. So first of all let us run callgrind to check where we spent most of our time: ...
Zeta's user avatar
  • 19.2k
10 votes

Unique value finder

Linear time solution Given that your values are restricted to the range [2000000..2200000], you can do the following: Iterate through your matrix and keep a count ...
JS1's user avatar
  • 28.5k
10 votes
Accepted

Calculating the Average Minimum Length of Random Numbers From 0 - 1 that Adds up to Greater Than 1

If I rewrite genSequence so that it takes numTrials as an argument, then I get the following timing in CPython: ...
Gareth Rees's user avatar
  • 49.6k
9 votes

Extending Sieve of Eratosthenes beyond a billion

Although it's best to use container classes in C++ instead of allocating memory manually, I'll still point this out since it should still be said: ...
Jamal's user avatar
  • 34.9k
9 votes

Improving gzip function for huge files

inF = gzip.open(FILE, 'rb') s = inF.read() inF.close() That reads the whole uncompressed data into memory. Of course it takes 60GB of memory. Looking ...
Peter Taylor's user avatar
  • 24.1k
9 votes

Intersection over Union for rotated rectangles

There are two main reasons why your program is slow and using huge amounts of memory: You are using a 768×768 pixel image for each overlap calculation You are checking each anchor box against each ...
G. Sliepen's user avatar
  • 50.3k
9 votes

Implementation of itoa which allocates the string

Counting the digits doubles the amount of work, and the division/modulo is indeed very hard work for the CPU. You know the maximum size of the resulting string, because it would be the largest or most ...
JDługosz's user avatar
  • 11.3k
9 votes
Accepted

Simple stack allocator in C

Bug: Invalid address computation StackPointer + length > Stack + sizeof(Stack) for this to be true, StackPointer + length ...
chux - Reinstate Monica's user avatar
8 votes
Accepted

Extending Sieve of Eratosthenes beyond a billion - follow-up

There are a few things you could do to improve your code. Use braces Constructions like this are a bug waiting to happen: ...
Edward's user avatar
  • 65.3k
8 votes

Frequency count of single words and word pairs over 20 million tweets

One smaller detail to add to PressingOnAlways' points: Optimize your loops Right now you're looping through the input file twice, once for FileType.OneWord and ...
BenC's user avatar
  • 2,738
8 votes

Frequency count of single words and word pairs over 20 million tweets

Your code looks fine, but the situation you are running into is because you are loading everything into memory and doing everything linearly. There are 3 aspects you can optimize your code which ...
PressingOnAlways's user avatar
8 votes
Accepted

Choose random instants in a day with a low memory footprint

1. Review There's a lot of repetition, especially of the number 86400. This can be avoided by defining a global constant, for example: ...
Gareth Rees's user avatar
  • 49.6k
8 votes
Accepted

In place solution to remove duplicates from a sorted list

One way that might speed up your solution slightly is to only place the value if necessary. Also note that only one of the two if conditions can be true, so just ...
Graipher's user avatar
  • 40.8k
7 votes
Accepted

Type-agnostic BubbleSort in C

Error handling Some would argue that it's unacceptable to exit() in case of an "out of memory" error occurring in a library function. I would invite them to tell ...
Rhymoid's user avatar
  • 186
7 votes
Accepted

Rotation of elements in an array

I see some things that may help you improve your program. Separate I/O from calculations The program has three basic phases. First, it gets input, then it manipulates that input, and then it ...
Edward's user avatar
  • 65.3k
7 votes

C++11 : simple Tree implementation

Encapsulation: My first question is why are you exposing TreeNode? Would the class that implemented a tree not be represented by ...
Martin York's user avatar
  • 94.3k
7 votes

Split a given number so that their sum adds to another given number

Python 2 Python 2 is end-of-life, and Python 3 has a lot of advantages now: f-strings unicode support ... More info here, here and on many other places. If you need python 2.7, try to make your ...
Maarten Fabré's user avatar
7 votes

Search father (x) of the number D(x) and measure time and memory consumed

PEP-8 Try to stick to PEP-8. So snake_case for variable and method names and a lot of other guidelines. Check any other Code ...
Maarten Fabré's user avatar
7 votes

Sparse 3D binning/histogram function

Some suggestions: Passing the code through at least one linter such as flake8 or pycodestyle will make for more idiomatic and therefore readable code. Do you have any automated tests? If so, do they ...
l0b0's user avatar
  • 8,887

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