29

Indeed you must have been working on this project for a while, since I answered a question on SO related to one of your old versions a while back. At least some of the feedback I had is still pertinent even now. Code and style comments #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <time.h&...


27

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> you get this optimization for free. Store only odd numbers in the array. This will save additional factor of 2 in memory. You need to adapt the logic of the program a little bit, but this is not ...


21

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 more context. What is the purpose of this code? What do these rotated rectangles represent? Tell us how the data computed in main relates to your problem. Is this ...


20

I try to manually dispose as much as I can This is usually good however this time you dispose too much. private void DocumentDrucker_PrintPage(object sender, PrintPageEventArgs e) { Graphics graphic = e.Graphics; .. graphic.Dispose(); // Don't! } You receive the Graphics object via the PrintPageEventArgs. This means that you should not ...


16

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 computer the time your algorithm takes is roughly equivalent to the amount of data that it reads and writes, so halving the space needed will half the execution ...


15

If it walks like an array and talks like an array, use it like an array. float avg(float *data, size_t length) { float sum = 0; for (size_t i = 0; i < length; i++) { sum += fabs(*(data + i)); } return (float) sum / length; } So, data is an array, right? Why don't we treat it like one and access it using subscripts? float ...


14

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. e.g: You create the same Point for printing at 4 different location (new Point(Convert.ToInt32(pageWidth * 0.35), offsetY + 37)) don't omit braces {} ...


14

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: static vector<uint64_t> *movie_map = new vector<uint64_t>[1200000]; static vector<uint64_t> *actor_movie_map = new vector<uint64_t>[2000000]; I'd suggest using std::...


12

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 #includes This program fragment requires two headers, which should be included but are not: #include <stdlib.h> // for getenv(), srand(), fprintf(), etc. #...


12

The most unforgivable fault with this function is that calling isValidBuffer() also erases the buffer! That completely violates the Principle of Least Surprise. If anything, the parameter should be const.


12

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 unprofessional, and is not really that helpful. To hell with sys.setrecursionlimit. It just delays the inevitable, rather than fixes your problem. Rather than using global ...


11

Goto conditions are often maligned as being bad practice... and, for a reason. In almost every situation where a goto is used, it's because you are doing too much in a method. Consider your main method: int main(void) { PaError err = paNoError; if((err = Pa_Initialize())) goto done; ..... AudioData data = initAudioData(44100, info->...


11

Design review Continuous reallocation of data.recordedSamples doesn't sound right. It may eventually fail because you require not just a lot of memory, but a lot of contiguous memory. Consider at least reusing data.recordedSamples instead of freeing it. That would require just one counter (how many blocks it may currently accomodate) and reduce amount of ...


11

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 using(): using (var graphics = new Graphics()) { // Your code here... } Also do not create a new Font for each page, reuse the same one for all pages. Maybe it's time to move this code into its own separate ...


10

First, you aren't using braces around one-line if statements: if (!outfile) return -1; Failing to use braces was the cause of Apple's SSL bug, as described here. Why are you doing this? #define FRAMES_PER_BUFFER 1024 Shouldn't that be static const int FRAMES_PER_BUFFER = 1024;? This SO question discusses this. As @RubberDuck mentioned in the comment, ...


10

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 of each of the numbers that you find in the matrix. You will need an array of size 200001 to keep these counts. Iterate through your count array. Whatever numbers have a count of 1 are unique. ...


10

If I rewrite genSequence so that it takes numTrials as an argument, then I get the following timing in CPython: Python 3.6.4 (default, Dec 21 2017, 20:33:21) >>> from timeit import timeit >>> timeit(lambda:genSequence(10**8), number=1) 2.71825759 62.77562193598715 Based on this, it would take about 10 minutes to compute genSequence(10**9)...


9

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 condition! Primary problem Your memory problem arises from launching a tremendous amount of goroutines. For example when you call compute() with n = 6 (to try passwords with a length of 6), it will ...


9

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: a = (bool*)malloc(MAX * sizeof(bool)); In C++, it's best to use new instead of malloc(). This will also avoid the need for such a cast, which is required in C++ not not in C. You can also initialize a instead ...


8

When writing programs it is often important to do some mental math before you start. In Math class at school they would often suggest that you estimate what the answer will be to a problem before you calculate it "for real". Here, what you have, is a recursive function. Each time the function is called, it loops over the current-list's members creates a new ...


8

Your primes_sum function seems to be inspired by the sieve of Erathostenes, but there are a number of things in your implementation that don't work out too well: Do not remove items from you num_list. That's an expensive operation which basically requires rewriting the whole list. Plus, it makes the following point impossible. If you have a list of ...


8

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 should give you pretty large gains: 1. Optimize Your Memory Utilization Consider Storing Your Data In a DB or datastore. For 20 million words, you can have a ...


8

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 again for TwoWord. You may see a speed improvement by interchanging these two operations, so you get something more like for line in reader { String[] words = lineToWords(line); ...


8

There are a few things you could do to improve your code. Use braces Constructions like this are a bug waiting to happen: for (unsigned long int k = i + j; k < MAX; k += j) if (a[k]) //Open the following comment very carefully //std::cout << "removes " << 2 * k + 1 << "\n"; a[k] = false; If anyone ever ...


8

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 at the documentation for gzip, it has this example of compressing a file: import gzip import shutil with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out: shutil....


8

1. Review There's a lot of repetition, especially of the number 86400. This can be avoided by defining a global constant, for example: _DAY_SECONDS = 24 * 60 * 60 # Seconds in a day. or alternatively: import datetime _DAY_SECONDS = datetime.timedelta(1).total_seconds() and then in mainloop: day, partday = divmod(int(time.time()), _DAY_SECONDS) ...


8

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 red box, but most of the time there is no overlap at all. Use exact solutions where possible For the first issue, instead of doing image manipulation, you can ...


8

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 use else. Or even better, since there was a continue in the other case, just don't indent it. i = 0 #slow-run pointer for j in range(1, len(nums)): if nums[j] == nums[i]: continue ...


7

Comments on your code: I’m surprised by the requirement that the input to answer() has to be a string, not an int. Since an int seems to be the natural input, I’d put most of the processing code in a separate function that only deals in ints, and do the string mangling at the very far edges of the program. Something like: def answer(str_S): result = ...


7

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 produces output. I would recommend putting the rotation code into a separate function. Consider signed vs. unsigned Does it make sense to have a negative array ...


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