[Java]
package com.company;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;
public class Bench {
public static void main(String[] args) throws IOException {
var start = System.currentTimeMillis();
parse();
var took = (System.currentTimeMillis() - start) / 1000f;
System.out.println(took);
System.out.println("Used memory: " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() / 1024 / 1024);
}
private static void parse() throws IOException {
var path = Paths.get("C:\\Users\\cosmi\\Desktop\\random_numbers.txt");
var charSet = StandardCharsets.UTF_8;
var sb = new StringBuilder();
try (var reader = Files.newBufferedReader(path, charSet)) {
String line;
while ((line = reader.readLine()) != null) {
var x = Double.parseDouble(line.split("\t")[0]);
var y = Double.parseDouble(line.split("\t")[1]);
double res = x / y;
sb.append(x).append("\t").append(y).append("\t").append(res).append("\n"); //desi lumea zice sa nu folosesti sb
}
}
writeResult(sb);
}
private static void writeResult(StringBuilder data) throws IOException {
var data1 = CharBuffer.wrap(data);
var pathToWrite = Paths.get("C:\\Users\\cosmi\\Desktop\\results_contest_devforum.txt");
try (FileChannel fileChannel = (FileChannel) Files
.newByteChannel(pathToWrite, EnumSet.of(
StandardOpenOption.READ,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING))) {
MappedByteBuffer mappedByteBuffer = fileChannel
.map(FileChannel.MapMode.READ_WRITE, 0, data1.length());
if (mappedByteBuffer != null) {
mappedByteBuffer.put(
StandardCharsets.UTF_8.encode(data1));
}
}
}
}
Dureaza aproximativ 12 secunde.