We’re rewarding the question askers & reputations are being recalculated! Read more.

# Tag Info

## Hot answers tagged io

103

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 function parameters - note, it's a parameter, not an argument, by the way)... is not conventional. On the other hand, I understand this is an exercise to test performance.... and it's not about ...

65

namespace std Others have said this already but it's important enough to be repeated: Don't put your own definitions into namespace std. It's undefined behavior. About the only thing I'm aware of that you may put into namespace std are specializations for templates already defined by the standard. So, for example, if you have struct MyType { int a; }; ...

53

Using BufferedInputStream would be a quick fix with lesser modification of current code. The reason why readX methods of DataInputStream/FileInputStream are slow is that they ask for IO every time. BufferedInputStream simply loads a chunk of file to reduce the number of IO operations needed. Another solution is to use read() method to load the (whole or ...

26

Although the C standard can allow void main() under certain implementations, it's best to use int main() whenever possible. See this for more info on the return type of main(). A loop is not a good use of a macro: #define printS for(i=0;s[i];i++){printf("%c",s[i]);} It should instead be a function: void printS() { for (i = 0; s[i]; i++) { ...

24

Great idea. I was having the same problem and this helped me solve it. Your method for doing cleanup though is wrong (as you mentioned it might be). Basically, you need to close the write end of the pipes after passing them to the subprocess. That way when the child process exits and closes it's end of the pipes, the logging thread will get a SIGPIPE and ...

23

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. Similarly, k can be moved once you've made your loop a for loop : for(int k = 1; k < 11; k++). Then, computer people love counting from 0. If you write : for(int k = 0; k < 10; k++), I am used to ...

21

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 double the size when it fills up. Of course this approach is less efficient memory wise, but in order to mitigate that, we can multiply by a smaller multiple (...

18

The issue is almost certainly in the structure of your Trace class, and it's memory efficiency. You should ensure that the instrType and hexAddress are stored as memory efficient structures. The instrType appears to be an int, which is good, but just make sure that it is declared as an int in the Trace class. The more likely problem is the size of the ...

18

Apart from the usage of a macro where a function would have been sufficient (don't use macros if there is another language construct that can do the job!) and the global variable (never do this unless it is absolutely needed), there is one big thing that has not yet been mentioned: Why do you print the string by calling printf() for every single character? ...

18

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's DataInputStream has high overhead for portability and error handling that is probably not needed in your situation. The fastest way to achieve your problem ...

17

First, keep track of your instances of a class: new BuzzwaySubs(); BuzzwaySubs.processCustomer(); That should be: BuzzwaySubs restaurant = new BuzzwaySubs(); // or var restaurant restaurant.processCustomer(); As it is, the first line is utterly useless. Additionally, this only works because your methods are all static. Keeping track of your ...

16

To steal an old quote: "There are 2 hard things in computer science. Naming, cache invalidation, and off-by-one errors". That being said, there is room for improvement here. Firstly, I'm assuming the class name, PPM, is short for Portable Pixmap Format. However, this isn't immediately obvious, and if you aren't familiar with that format (I'm not), it ...

16

I have found a couple of things that could help you improve your code. Don't abuse using namespace std Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. Avoid the use of global variables I see that a and b are declared as global variables rather than as local variables. It's generally better to ...

16

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, and an empty vector is likely 24 bytes in size. So this is huge waste. Why not use a container designed to hold a series of ASCII characters, like... std::string! Do you need to store all ...

15

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 that a for loop makes more sense in this particular case. for(int k = 0; k < 10; k++) { cout << "\nEnter any number other than 5: "; cin >&...

15

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 than a java.io.File. List<String> wordList = new ArrayList(); should be List<String> wordList = new ArrayList<>(); to suppress a compiler warning. The readUrlTextContent() ...

15

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 today. The version of C it teaches has been obsolete for decades. I don't know which edition you're using (looks like a more recent one by the style of code) ...

15

/* 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 function doesn't ask for input. (As written it is not responsible for printing the prompt.) Perhaps also should clarify the intended behavior: If there is input that is not terminated by a newline, ...

14

Prefer the C++ way As you seem to expect, one of the key issues with your code is that it's not very C++y. 1. Prefer std::string over C-style strings. std::string is better in almost every way. It takes care of its own memory and grows when needed. The speed is usually comparable to C-style strings (sometimes faster, sometimes slower). The code written ...

14

from urllib import urlopen from gzip import GzipFile from cStringIO import StringIO import re import urllib import urllib2 import webbrowser import mechanize import time from difflib import SequenceMatcher import os def download(url): s = urlopen(url).read() if s[:2] == '\x1f\x8b': # assume it's gzipped data with GzipFile(mode='rb', fileobj=...

14

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 optimizer is turned on. Rust loops have terrible performance in debug mode. Turning the optimizer on should probably get you near 25 MB/sec or so, at least in my ...

14

In splitTextStringIntoWordList I found myself having to split textString param into String[] array, and immediately afterwards adding the elements from the array one at a time, parsing with regex, into a List<String>. Is there a better way to do this that might not need as much manipulation? Wells, in Java 8... List<String> result = Pattern....

14

Don't do this: *c = realloc(*c, sizeof(**c) * (*capacity)); Once you have error handling that's more sophisticated than exit(1), this will become a liability. You need a temporary: char *tmp = realloc(*c, new_capacity); if (!tmp) { /* error handling - c is still valid */ /* ... */ } *c = tmp; *capacity = new_capacity; If you always initialize ...

13

The Design A LogReader reads a log, filters the unwanted stuff and converts it into LogEntries. According to your design. That's a bit much. Split it up. Make one LogReader that just reads the file and presents you with endless strings. Oh wait, that's a BufferedReader! I'd see the LogReader basically as an iterator wrapper for BufferedReader. Make a ...

13

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: >>> benchmark = lambda:decompress(open('image.compressed', 'rb')) >>> from timeit import timeit >>> timeit(benchmark, number=1) 80....

12

Actually, this can be more generic. Instead of hard-coding your map; fill it from your properties file. So change your prop file to : #db.id=a #db.user=testa #db.password=testa db.id=b db.user=testb db.password=testb Fill your map like this : private Map<String, String> schemaMap = new HashMap<String, String>(); public void fillMap() ...

12

Error Handling Your error handlers look much cleaner (and ultimately safer) than before. I also like your GetErrorSoure() routine and the "CleanExit:" name of the labels. Very concise. (<-Read as, "I'll be 'borrowing' more of your code'). I see a small issue in your OpenFile() routine. You should probably set OpenFile = False prior to re-raising the ...

12

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 compiler is C++11 capable (which is probably the case): // Add these somewhere: #include <array> #include <string> ... const std::array<std::string, 5> choices = { "I don't ...

12

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: struct Beverage { const std::string name; const std::string message; Beverage(const std::string& name, const std::string& message): name(name),...

12

Have you heard about Turkey? Try inserting the following line at the top of your main: java.util.Locale.setDefault(java.util.Locale.forLanguageTag("tr-TR")); Now you output will look a little different: [0] THE [1] ANT [2] AND [3] THE [4] CRICKET [5] ONCE [6] UPON [7] A [8] T ... [351] WELL [352] TRY [353] DANC [354] NOW In the turkish locale "i"....

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