Created
April 2, 2016 12:51
-
-
Save jnizet/21341d48f631b7f10bc657e560c0f2de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.MappedByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.util.Random; | |
public class TestWCJava2 { | |
public static void main(String args[]) throws IOException { | |
// generateFile(); | |
try { | |
new TestWCJava2().run(args); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void generateFile() throws IOException { | |
Random r = new Random(); | |
byte[] buffer = new byte[1024]; | |
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/tmp/three"))) { | |
for (int i = 0; i < 15 * 1024; i++) { | |
r.nextBytes(buffer); | |
bos.write(buffer); | |
} | |
} | |
} | |
void run(String files[]) throws IOException { | |
long t0 = System.nanoTime(); | |
wordCount("/tmp/three"); | |
long t1 = System.nanoTime(); | |
System.out.println("Elapsed time: " + (t1 - t0) / 1000 + "us"); | |
} | |
int wordCount(String fileName) throws IOException { | |
File f = new File(fileName); | |
FileChannel fc = new FileInputStream(f).getChannel(); | |
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); | |
int nlines = 0; | |
byte newline = '\n'; | |
byte[] buffer = new byte[4096]; | |
while(mem.hasRemaining()) { | |
int read = Math.min(mem.remaining(), buffer.length); | |
mem.get(buffer); | |
for (int i = 0; i < read; i++) { | |
if (buffer[i] == newline) { | |
nlines++; | |
} | |
} | |
} | |
System.out.println(fileName + " lines: " + nlines); | |
return nlines; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment