50 मिलियन विशेष रूप से बड़ा नहीं है। मैं उन्हें स्मृति में पढ़ूंगा। उन्हें क्रमबद्ध करें और उन्हें लिखें। इसमें कुछ ही सेकंड लग सकते हैं। आपको इसकी कितनी तेज़ी से आवश्यकता है? आपको इसकी आवश्यकता कितनी जटिल है?
मेरी पुरानी प्रयोगशाला में 28 सेकंड लग गए। अगर मेरे पास अधिक प्रोसेसर थे, तो यह थोड़ा तेज़ हो सकता है लेकिन फ़ाइल को पढ़ने और लिखने में अधिक समय लगता है (15 सेकंड) जो कि तेज़ नहीं होगा।
महत्वपूर्ण कारकों में से एक आपके कैश का आकार है। तुलना स्वयं बहुत ही सस्ता है बशर्ते डेटा कैश में हो। चूंकि एल 3 कैश साझा किया जाता है, इसलिए एक थ्रेड आपको इसका पूर्ण उपयोग करने की आवश्यकता होती है।
public static void main(String...args) throws IOException {
generateFile();
long start = System.currentTimeMillis();
int[] nums = readFile("numbers.bin");
Arrays.sort(nums);
writeFile("numbers2.bin", nums);
long time = System.currentTimeMillis() - start;
System.out.println("Took "+time+" secs to sort "+nums.length+" numbers.");
}
private static void generateFile() throws IOException {
Random rand = new Random();
int[] ints = new int[50*1000*1000];
for(int i= 0;i<ints.length;i++)
ints[i] = rand.nextInt();
writeFile("numbers.bin", ints);
}
private static int[] readFile(String filename) throws IOException {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filename), 64*1024));
int len = dis.readInt();
int[] ints = new int[len];
for(int i=0;i<len;i++)
ints[i] = dis.readInt();
return ints;
}
private static void writeFile(String name, int[] numbers) throws IOException {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(name), 64*1024));
dos.writeInt(numbers.length);
for (int number : numbers)
dos.writeInt(number);
dos.close();
}
स्रोत
2010-11-27 12:43:19
:) आप क्या कहना चाहते हैं? –
@ पॉल वह सिर्फ मैट्रिक्स से है - उसका उपनाम देखें :) –
आप मानक एल्गोरिदम का उपयोग क्यों नहीं कर सकते? क्या यह एक होमवर्क समस्या है? –