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 ...
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
...
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 ...
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'...
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 ...
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, ...
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),...
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
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 ...
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:
...
10
votes
Accepted
Network Interface Object
There is no guarantee that write writes out the entire buffer. This means that
...
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 ...
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 ...
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 <...
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 ...
9
votes
Taking arbitrary length input in C
Functionality
Error: input(stdin, 0) could quickly return NULL as code incorrectly assumes a ...
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 ...
9
votes
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 ...
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 ...
9
votes
Input validation
Non-exported functions
Declare get_valid_input and flush as static since you're in a single ...
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 ...
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 ...
8
votes
Accepted
Delete duplicate files
To search files you're using Directory.GetFiles(), nothing wrong with that but, especially in conjunction with ...
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 ...
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 ...
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:
...
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 ...
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 ...
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 (...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
io × 496c × 134
java × 100
c++ × 84
beginner × 84
performance × 70
file × 62
python × 61
strings × 50
formatting × 28
c# × 27
parsing × 20
error-handling × 19
stream × 19
python-3.x × 18
console × 18
javascript × 15
programming-challenge × 15
go × 15
comparative-review × 14
validation × 14
file-system × 14
c++11 × 13
python-2.x × 13
haskell × 12