2014-02-25 42 views
0

我試圖在200,000個文本文件中執行搜索,其大小可能從50kb到5mb不等,總共爲1.7GB。我打算開發一個搜索引擎(只是一個樣本)。 過程是:在一個性能良好的文件中搜索

1) Extract words from each file and store them in a separate file(40,000,000 words) 
2) Search each word in each file (40,000,000(words) X 200,000(Files) = 8 X 10^12 searches) 
3) Generate boolean Index(650Mb). 

所以,大部分這裏所涉及的操作都將在文件(S)或文件(縣)搜索。 (4+小時)

這是我編寫的用於在JAVA中搜索單詞的程序。

count = 0; 
BufferedReader reader = new BufferedReader(new FileReader('fileName.txt')); 
while ((text = reader.readLine()) != null) { 
if(text.indexOf(searchString) != -1) 
{ 
    if(text.equals(searchString)) 
    { 
     System.out.print('Word Found in line number '+count); 
     break; 
    } 
} 
count++; 
} 

計劃在Python:

count = 0 
file = open(filePath) 
with file as f : 
    for line in f: 
     count += 1 
     if(line.index(searchWord)) 
      print("Word found in line number"+count) 

輸出是完美的,但它需要大量的時間。語言對我來說並不是一個考慮的標準。我正在尋找更好的表現。有沒有什麼辦法可以解決這個問題。由於它大部分是搜索過程,是否有任何完美的方式,因爲它正在搜索大塊小塊。

(我的電腦配置:8GB內存,i7處理器第四代)

+0

所以你試圖在Java中實現'grep'? – devnull

+1

你爲什麼在Java和Python中工作?您想要哪種語言的解決方案? – wnnmaw

+1

你需要編寫一個程序嗎?或者你只需​​要尋找一個工具來做到這一點? – Derek

回答

3

您可以將文件分割成多個塊&然後處理平行使用不同的線程塊的那些。 (類似的Map Reduce)

例子:在每個100MB的塊分割的文件(比如說有17塊)

現在,你可以通過這些數據塊到各個線程,然後搜索的文本。

public class SearchText 
{ 

    public void processFile() 
    { 
    List<Chunks> totalChunks = splitFile(); 
    // you have to implement splitFile() function to split file in chunks 

    for(Chunks chunk : totakChunks) 
    { 
     // Create a new Thread and process the chunks 
     new Thread(new ChunkProcessor(chunk)).start(); 
    } 
    } 
} 

public class ChunkProcessor implements Runnable 
{ 

    private Chunk mychunk ; 
    public ChunkProcessor(Chunk chunk) 
    { 
    myChunk = chunk; 
    } 


    public void run() 
    { 
     // search for text in this chunk 
    } 
} 
+1

後面的實際概念(雖然不是Python,但是......) – geoffspear

+0

@Wooble,可能很好的說明你可以在Python中完成它,但它不會幫助任何 – wnnmaw

+0

分割文件通常是按順序完成的並且通常只要閱讀就可以了。 –

0

你可以嘗試建立使用Trie數據結構,然後再執行它的搜索索引。

1

運行我可以買它運行Windows的最便宜的筆記本電腦之一7.

public class SearchTestMain { 
    public static void main(String[] args) throws IOException { 
     File file = new File("deleteme.txt"); 
     PrintWriter pw = new PrintWriter(file); 
     Random rand = new Random(); 
     int numbers = 42 * 1000 * 1000; 
     long start = System.currentTimeMillis(); 
     System.out.println("Writing " + file); 
     // average line length ~36 bytes. 
     for (int i = 0; i < numbers; i++) { 
      pw.println(rand.nextLong() & Long.MAX_VALUE); // positive only 
      pw.println(rand.nextLong() & Long.MAX_VALUE); // positive only 
     } 
     pw.close(); 
     long mid = System.currentTimeMillis(); 

     System.out.println("Reading " + file); 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String searchTerm = "31415926"; 
     for (String line; ((line = br.readLine())) != null;) 
      if (line.contains(searchTerm)) 
       System.out.println("found " + searchTerm + " in " + line); 
     br.close(); 
     long end = System.currentTimeMillis(); 
     System.out.printf("Writing took %.1f seconds, reading took %.1f seconds for a %,d MB file%n", 
       (mid - start)/1e3, (end - mid)/1e3, file.length()/1000000); 
     file.delete(); 
    } 
} 

打印

Writing deleteme.txt 
Reading deleteme.txt 
found 31415926 in 6728531415926595287 
found 31415926 in 8919165331415926916 
... some deleted ... 
found 31415926 in 2826331415926854237 
found 31415926 in 5676780473141592623 
Writing took 35.5 seconds, reading took 55.1 seconds for a 1,753 MB file 

我是,如果閱讀是很驚訝,搜索單獨文本花費比一個更分鐘。如果花費更長時間,它正在做一些你沒有告訴我們的事情。

+0

是的。這不是程序的全部想法,我還有其他幾個任務要執行,並在不同的文件中搜索基本想法。主要目的是實現布爾型​​索引並開發一個搜索引擎 – user1919035

+0

@ user1919035,如果你總共花費10分鐘,我懷疑這個額外的東西是另外9分鐘。即閱讀/搜索只是你問題的10%。 –

+0

@ user1919035我建議你使用CPU /內存分析器來查看它大部分時間都在做什麼。沒有測量,我發現我只是猜測;) –

相關問題