2014-02-25 33 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 /内存分析器来查看它大部分时间都在做什么。没有测量,我发现我只是猜测;) –

相关问题