मैं हाल ही में आईओ प्रदर्शन का पता लगाने के प्रयास कर रहे हैं हो सकता है। मैंने जो देखा है, उससे सीधे FileOutputStream
पर लिखने से बेहतर परिणाम सामने आए हैं; जिसे मैंने के लिए FileOutputStream
के मूल कॉल के लिए जिम्मेदार ठहराया है। इसके अलावा, मैंने यह भी देखा है कि जब BufferedOutputStream
की विलंबता सीधे FileOutputStream
की ओर बढ़ने लगती है, तो यह बहुत अधिक उतार-चढ़ाव करता है यानी यह अचानक भी डबल-अप हो सकता है (मैं अभी तक क्यों नहीं ढूंढ पा रहा हूं)।
पीएस मैं जावा 8 का उपयोग कर रहा हूं और अभी इस पर टिप्पणी नहीं कर पाऊंगा कि मेरे अवलोकन पिछले जावा संस्करणों के लिए होंगे या नहीं।
यहाँ कोड मैं परीक्षण किया है, जहां मेरे इनपुट एक ~ 10KB फ़ाइल थी
public class WriteCombinationsOutputStreamComparison {
private static final Logger LOG = LogManager.getLogger(WriteCombinationsOutputStreamComparison.class);
public static void main(String[] args) throws IOException {
final BufferedInputStream input = new BufferedInputStream(new FileInputStream("src/main/resources/inputStream1.txt"), 4*1024);
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int data = input.read();
while (data != -1) {
byteArrayOutputStream.write(data); // everything comes in memory
data = input.read();
}
final byte[] bytesRead = byteArrayOutputStream.toByteArray();
input.close();
/*
* 1. WRITE USING A STREAM DIRECTLY with entire byte array --> FileOutputStream directly uses a native call and writes
*/
try (OutputStream outputStream = new FileOutputStream("src/main/resources/outputStream1.txt")) {
final long begin = System.nanoTime();
outputStream.write(bytesRead);
outputStream.flush();
final long end = System.nanoTime();
LOG.info("Total time taken for file write, writing entire array [nanos=" + (end - begin) + "], [bytesWritten=" + bytesRead.length + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("File reading result was: \n" + new String(bytesRead, Charset.forName("UTF-8")));
}
}
/*
* 2. WRITE USING A BUFFERED STREAM, write entire array
*/
// changed the buffer size to different combinations --> write latency fluctuates a lot for same buffer size over multiple runs
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("src/main/resources/outputStream1.txt"), 16*1024)) {
final long begin = System.nanoTime();
outputStream.write(bytesRead);
outputStream.flush();
final long end = System.nanoTime();
LOG.info("Total time taken for buffered file write, writing entire array [nanos=" + (end - begin) + "], [bytesWritten=" + bytesRead.length + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("File reading result was: \n" + new String(bytesRead, Charset.forName("UTF-8")));
}
}
}
}
उत्पादन:
2017-01-30 23:38:59.064 [INFO] [main] [WriteCombinationsOutputStream] - Total time taken for file write, writing entire array [nanos=100990], [bytesWritten=11059]
2017-01-30 23:38:59.086 [INFO] [main] [WriteCombinationsOutputStream] - Total time taken for buffered file write, writing entire array [nanos=142454], [bytesWritten=11059]
स्रोत
2017-01-30 15:53:29
कुछ मैं अभी तक नहीं मिला है - BufferedOutputStream के डिफ़ॉल्ट बफर आकार क्या है जावा 6 में? आप 8 केबी का जिक्र करते हैं - क्या जावा में डिफ़ॉल्ट है? 1.4.2 के लिए जावाडॉक्स का कहना है कि बफर 512 बाइट्स है, जिसका अर्थ है कि मैं जो कुछ लिखता हूं वह 200 से 400 बाइट प्रति सरणी के बीच गिरता है। हालांकि, यह जानकारी जावा 6 दस्तावेज से हटा दी गई है। –
@ थॉमस - [स्रोत कोड को देख रहे हैं] (http://www.docjar.com/html/api/java/io/BufferedOutputStream.java.html#51), डिफ़ॉल्ट आकार 8192 है। मुझे लगता है कि वे एक नया "सबसे समझदार डिफ़ॉल्ट" प्रकट होने पर इसे बदलने में सक्षम होने के लिए डिफ़ॉल्ट आकार विनिर्देश को हटा दिया गया। यदि कोई विशिष्ट बफर आकार महत्वपूर्ण है, तो आप शायद इसे स्पष्ट रूप से निर्दिष्ट करना चाहते हैं। – gustafc
@gustafc धन्यवाद। मैं हमेशा भूल जाता हूं कि मैं जावा स्रोत कोड देख सकता हूं। –