मेरे जावा कोड का मूल्यांकन करते समय मुझे एक बड़ी समस्या है। समस्या को सरल बनाने के लिए मैंने निम्नलिखित कोड लिखा जो समान उत्सुक व्यवहार उत्पन्न करता है। महत्वपूर्ण तरीका है() और डबल वैल्यू रेट दिया गया है। मेरे रनटाइम टेस्ट (मुख्य विधि में) के लिए मैंने दर को 0.5 बार एक बार और 1.0 बार सेट किया। मान 1.0 के साथ यदि प्रत्येक लूप पुनरावृत्ति में if-statement निष्पादित किया जाएगा और मान 0.5 के साथ if-statement को आधे से अधिक निष्पादित किया जाएगा। इस कारण से मुझे पहले मामले से लंबे समय तक रनटाइम की उम्मीद थी लेकिन विपरीत सच है। क्या कोई मुझे इस घटना की व्याख्या कर सकता है ??जावा उत्सुक लूप प्रदर्शन
मुख्य का परिणाम:
Test mit rate = 0.5
Length: 50000000, IF executions: 25000856
Execution time was 4329 ms.
Length: 50000000, IF executions: 24999141
Execution time was 4307 ms.
Length: 50000000, IF executions: 25001582
Execution time was 4223 ms.
Length: 50000000, IF executions: 25000694
Execution time was 4328 ms.
Length: 50000000, IF executions: 25004766
Execution time was 4346 ms.
=================================
Test mit rate = 1.0
Length: 50000000, IF executions: 50000000
Execution time was 3482 ms.
Length: 50000000, IF executions: 50000000
Execution time was 3572 ms.
Length: 50000000, IF executions: 50000000
Execution time was 3529 ms.
Length: 50000000, IF executions: 50000000
Execution time was 3479 ms.
Length: 50000000, IF executions: 50000000
Execution time was 3473 ms.
कोड
public ArrayList<Byte> list = new ArrayList<Byte>();
public final int LENGTH = 50000000;
public PerformanceTest(){
byte[]arr = new byte[LENGTH];
Random random = new Random();
random.nextBytes(arr);
for(byte b : arr)
list.add(b);
}
public void run(double rate){
byte b = 0;
int count = 0;
for (int i = 0; i < LENGTH; i++) {
if(getRate(rate)){
list.set(i, b);
count++;
}
}
System.out.println("Length: " + LENGTH + ", IF executions: " + count);
}
public boolean getRate(double rate){
return Math.random() < rate;
}
public static void main(String[] args) throws InterruptedException {
PerformanceTest test = new PerformanceTest();
long start, end;
System.out.println("Test mit rate = 0.5");
for (int i = 0; i < 5; i++) {
start=System.currentTimeMillis();
test.run(0.5);
end = System.currentTimeMillis();
System.out.println("Execution time was "+(end-start)+" ms.");
Thread.sleep(500);
}
System.out.println("=================================");
System.out.println("Test mit rate = 1.0");
for (int i = 0; i < 5; i++) {
start=System.currentTimeMillis();
test.run(1.0);
end = System.currentTimeMillis();
System.out.println("Execution time was "+(end-start)+" ms.");
Thread.sleep(500);
}
}
http://stackoverflow.com/questions/11227809/why- है प्रसंस्करण एक हल कर सरणी-तेजी-से-एक-अवर्गीकृत सरणी)। – assylias
यादृच्छिक यहां बहुत धीमी है। मेरा सुझाव है कि आप एक सरल प्रगति का पालन करें और आप अपने अधिकांश समय यादृच्छिक संख्या उत्पन्न नहीं करेंगे। मैं आपको कई बार चलाने और दूसरी बार कई बार चलाने के बजाय वैकल्पिक परीक्षण का सुझाव देता हूं। (आपको उनके बीच सोने की जरूरत नहीं है) –
गर्म करने में सोच रहा है कि क्या आप पहले 1.0 निष्पादित करते हैं? – ssedano