snippetjavaMinor
Generate Random String and Writing to File
Viewed 0 times
randomfilewritinggenerateandstring
Problem
my task is to generate random strings and store in a file till the size of file is less than 10MB. My approach towards this problem is as follows.
It takes about 100.495 seconds to complete on my i3 processor with 4GB of RAM. So my question is, how to improve this performance? Why is the performance so poor? Is there any another workaround for this?
import java.io.File;
import java.io.FileWriter;
public class Application {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
File file = new File("Hello.txt");
file.createNewFile();
FileWriter writer = new FileWriter(file);
while (file.length() <= 1e+7) {
writer.write(Generator.generateRandomString(12, Generator.Mode.ALPHANUMERIC));
writer.write("\n");
writer.write(Generator.generateRandomString(12, Generator.Mode.NUMERIC));
writer.write("\n");
writer.write(Generator.generateRandomString(12, Generator.Mode.ALPHA));
writer.write("\n");
}
writer.flush();
writer.close();
long end = System.currentTimeMillis();
System.out.println((end - start) / 1000f + " seconds");
}
}It takes about 100.495 seconds to complete on my i3 processor with 4GB of RAM. So my question is, how to improve this performance? Why is the performance so poor? Is there any another workaround for this?
Solution
It's the repeated calls to
Let's try this
On my machine that took ~51s.
Now let's try this:
Approximately 0.3s. And a quick sanity check:
There's a problem with the original method, which is the interaction of
We actually end up with a lot more because of the buffered writer:
A similar problem is also present in the method presented here: we end up writing 403 bytes, not because of buffering but because of how we're counting. If you want to be precise about the total file size, you will need to modify it.
file.length() that is slowing you down.Let's try this
while (file.length() <= 1e+7) {
writer.write("abcdefghijkl");
writer.write("\n");
writer.write("abcdefghijkl");
writer.write("\n");
writer.write("abcdefghijkl");
writer.write("\n");
}On my machine that took ~51s.
Now let's try this:
for (int length = 0; length <= 1e+7; length += 39) {
writer.write("abcdefghijkl");
writer.write("\n");
writer.write("abcdefghijkl");
writer.write("\n");
writer.write("abcdefghijkl");
writer.write("\n");
}Approximately 0.3s. And a quick sanity check:
$ du -h Hello.txt
9.6M Hello.txtThere's a problem with the original method, which is the interaction of
file.length() and using a buffered writer. Let's try to write up to 400 bytes:while (file.length() <= 400) {
writer.write("abcdefghijkl");
writer.write("\n");
}We actually end up with a lot more because of the buffered writer:
$ du -b Hello.txt
8203 Hello.txtA similar problem is also present in the method presented here: we end up writing 403 bytes, not because of buffering but because of how we're counting. If you want to be precise about the total file size, you will need to modify it.
Code Snippets
while (file.length() <= 1e+7) {
writer.write("abcdefghijkl");
writer.write("\n");
writer.write("abcdefghijkl");
writer.write("\n");
writer.write("abcdefghijkl");
writer.write("\n");
}for (int length = 0; length <= 1e+7; length += 39) {
writer.write("abcdefghijkl");
writer.write("\n");
writer.write("abcdefghijkl");
writer.write("\n");
writer.write("abcdefghijkl");
writer.write("\n");
}$ du -h Hello.txt
9.6M Hello.txtwhile (file.length() <= 400) {
writer.write("abcdefghijkl");
writer.write("\n");
}$ du -b Hello.txt
8203 Hello.txtContext
StackExchange Code Review Q#72094, answer score: 3
Revisions (0)
No revisions yet.