关于lucene的RAMDirectory和FSDirectory的性能问题的困惑

在lucene in Action书中说RAMDirectory的性能总是比FSDirectory优越(书中2.7.2章节)
并附了测试用例
我根据测试用例去实际测试了一下,结果是相反的
这让我很困惑,内存没道理比文件系统慢啊。。
附上执行结果:
RAMDirectory Time: 500 ms
FSDirectory Time: 266 ms

以下是我的代码(基本照搬书中例子,只更改了for循环写法和使用了2.9的推荐方法取代了老版本的方法)

 

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import junit.framework.TestCase;

/**
* 测试FSDirectory和RAMDirectory之间的性能差异
* 理论上来说,后者应大于前者,但是实际测试值相反,why? lucifer 2010-5-10
* @author lucifer
*
*/
public class FSversusRAMDirectoryTest extends TestCase
{
private Directory fsDir;
private Directory ramDir;
private Collection<String> docs = loadDocuments(3000,5);

protected void setUp() throws Exception
{
String fsIndexDir = System.getProperty("java.io.tmpdir","tmp")+File.separator+"fs-index";
ramDir = new RAMDirectory();
fsDir = FSDirectory.open(new File(fsIndexDir));
}

public void testTiming() throws IOException
{
long ramTiming = timeIndexWriter(ramDir);
long fsTiming = timeIndexWriter(fsDir);

// assertTrue(fsTiming>ramTiming);

System.out.println("RAMDirectory Time: "+ ramTiming +" ms");
System.out.println("FSDirectory Time: "+ fsTiming +" ms");
}

private long timeIndexWriter(Directory dir)throws IOException
{
long start = System.currentTimeMillis();
addDocuments(dir);
long stop = System.currentTimeMillis();
return (stop - start);
}

private void addDocuments(Directory dir)throws IOException
{
/**
* SimpleAnalyzer:把所有字符过滤成小写
* 把参数设为false时,使用RAMDirectory出错,报文件找不到
*/
IndexWriter writer = new IndexWriter(dir,new SimpleAnalyzer(),true,IndexWriter.MaxFieldLength.UNLIMITED);

/**
* 以下参数影响FSDirectory性能
* MergeFactor的值不能小于2
* MaxMergeDocs的值可以设置的比MergeFactor小,未见异常抛出
*/
writer.setMergeFactor(10);
writer.setMaxMergeDocs(10000);

for(Object obj:docs)
{
Document doc = new Document();
String word = (String)obj;
doc.add(new Field("keyword",word,Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("unindexed",word,Field.Store.YES,Field.Index.NO));
doc.add(new Field("unstored",word,Field.Store.NO,Field.Index.NOT_ANALYZED));
doc.add(new Field("text",word,Field.Store.NO,Field.Index.ANALYZED));
writer.addDocument(doc);
}
writer.optimize();
writer.close();
}

private Collection<String> loadDocuments(int numDocs,int wordsPerDoc)
{
Collection<String> docs = new ArrayList<String>(numDocs);
for(int i=0;i<numDocs;i++)
{
StringBuffer doc = new StringBuffer(wordsPerDoc);
for(int j=0;j<wordsPerDoc;j++)
doc.append("bibamus ");
docs.add(doc.toString());
}

return docs;
}
}

(转自:http://www.iteye.com/problems/42016)

关于lucene的RAMDirectory和FSDirectory的性能问题的困惑的更多相关文章

  1. 基于Lucene查询原理分析Elasticsearch的性能

    前言 Elasticsearch是一个很火的分布式搜索系统,提供了非常强大而且易用的查询和分析能力,包括全文索引.模糊查询.多条件组合查询.地理位置查询等等,而且具有一定的分析聚合能力.因为其查询场景 ...

  2. lucene中FSDirectory、RAMDirectory的用法

    package com.ljq.one; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStrea ...

  3. Lucene的分析资料【转】

    Lucene 源码剖析 1 目录 2 Lucene是什么 2.1.1 强大特性 2.1.2 API组成- 2.1.3 Hello World! 2.1.4 Lucene roadmap 3 索引文件结 ...

  4. Lucene学习笔记

    师兄推荐我学习Lucene这门技术,用了两天时间,大概整理了一下相关知识点. 一.什么是Lucene Lucene即全文检索.全文检索是计算机程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明 ...

  5. lucene、lucene.NET详细使用与优化详解

    lucene.lucene.NET详细使用与优化详解 2010-02-01 13:51:11 分类: Linux 1 lucene简介1.1 什么是luceneLucene是一个全文搜索框架,而不是应 ...

  6. Lucene入门的基本知识(四)

    刚才在写创建索引和搜索类的时候发现非常多类的概念还不是非常清楚,这里我总结了一下. 1 lucene简单介绍  1.1 什么是lucene  Lucene是一个全文搜索框架,而不是应用产品.因此它并不 ...

  7. lucene 使用教程

    原文转自:http://cloudera.iteye.com/blog/656459 1 lucene简介  1.1 什么是lucene  Lucene是一个全文搜索框架,而不是应用产品.因此它并不像 ...

  8. 【转】Lucene.NET详细使用与优化详解

    1 lucene简介1.1 什么是luceneLucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿来就能用,它只是提供了一种工 ...

  9. lucene使用与优化

    lucene使用与优化 1 lucene简介 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿 ...

随机推荐

  1. xubuntu openocd nRF51822 download --- 2

    昨天非常晚的时候才最终发现事实上Unkown USB Device并非错误,仅仅是个警告而已,所以我们不关心就能够.让Makefile继续往下走就能够.于是我尝试mbs,s110.cload和firm ...

  2. MySQL Win7 64位 下载、安装与配置图文教程

    MySQL是目前十分流行的一种关系型数据库管理系统.官网推出的安装包有两种格式,分别是:ZIP格式和MSI格式.其中MSI格式的可以直接点击安装,按照它给出的安装提示进行安装,一般MySQL将会安装在 ...

  3. javascript - 一种对象赋值方式

    /** * step1: * 结果为:1,'2' */ let opt = { num: 1, str: '2' } let { num, str } = opt; console.log(num, ...

  4. 响应式设计工具网站mydevice.io

    1.网址 https://mydevice.io/ 2.使用 在mydevice.io上有常见智能手机,PC电脑的尺寸.

  5. scrollBy 相对滚动

    scrollBy可以相对当前位置移动滚动条,而不是移动到绝对位置 scrollBy(0, 100); // 滚动条下移100px

  6. Odoo8在TreeView左上角增加自定义按钮以及通过继承生成自定义的View_Mode

    今天有网友在问怎么在TreeView左上角增加一个自定义的按钮,在查询Odoo 自带的模块,发现在purchase_requisition中有使用,并且此模块还应用到了自定义View_Mode的情况, ...

  7. QT调用C#写的Dll

    参见: https://blog.csdn.net/weixin_42420155/article/details/81060945 C#写的dll是没有dllMain入口函数的,是一种中间语言,需要 ...

  8. 安装Drupal7.12+Postgresql9.1(Ubuntu Server 12.04)

    怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ OS环境准备 这次是从OS开始安装的.最开始装Ubuntu12.04这里就不再赘述, 唯 ...

  9. chrome扩展(浏览器插件)开发实用教程

    原创文章,转载请注明出处. 作者:简体字丶冯; QQ:564372931 1.Chrome能搞这些事情 (1)     操作浏览器中打开的页面DOM 这能做什么哪?譬如说你想修改页面DOM(DOM是什 ...

  10. C语言基础(20)-文件操作(fopen,getc,fclose)

    一.文件操作 读文件的顺序: 1.先打开文件 2.读写文件 3.关闭文件 1.1 fopen FILE *fopen( const char *path, const char *mode ); 函数 ...