我一直在尝试加快从 PDF 文件中提取文本的速度。我试过 SnowTide 的PDFTextStream
. 它比我电脑上的 iText 慢。
我最近的尝试是使用多个线程。
PdfReader
我用字节数组而不是文件名初始化。这样我就可以通过将其全部拉入内存来避免 IO 延迟。据我所知,iText 根据需要从文件中获取数据。使用字节数组,它不应该有这个问题。
我使用线程来提取多个页面范围。
但是,这使它变慢了很多。使用一个线程与五个线程相比更快。我不知道为什么会这样。据我所知,bottlneck 正在提取页面本身PdfTextExtractor.getTextFromPage(mReader, i)
。我对连接线程文本的 for 循环进行了计时,这不会影响总时间。
我的问题是,为什么它变慢了,我怎样才能让它更快?
访问同样PdfReader
会造成瓶颈吗?我应该PdfReader
用相同的字节数组初始化多个吗?我对我的任何研究有误吗?如果有人对全新的开源 PDF 提取器或其他建议有建议,那就太好了!
import java.io.*;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.PdfReader;
import java.nio.channels.FileChannel;
import java.nio.MappedByteBuffer;
public class PDFParser
{
private PdfReader mReader;
private int numPages;
private String bodyText;
public PDFParser(String pdf)
throws IOException
{
/*
scrapped this
mReader = new PdfReader(pdf);
and replaced with the following.
From what I can tell Pdfreader with the above constructor
probably uses a stream to get the pages one at a time.
I want to load it all into memory to increase the speed.
*/
final FileChannel channel = new FileInputStream(pdf).getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
byte[] byteArray = new byte[buffer.remaining()];
buffer.get(byteArray);
channel.close();
mReader = new PdfReader(byteArray);
numPages = mReader.getNumberOfPages();
bodyText = "";
}
public String parse()
{
int numThreads = 5;
int offset = (numPages / numThreads) - 1;
int currentFirstPage = 1;
ParserThread[] threads = new ParserThread[numThreads];
int forEnd = numThreads - 1;
for (int i = 0; i < forEnd; i++)
{
threads[i] = new ParserThread(mReader, currentFirstPage, currentFirstPage + offset);
threads[i].start();
currentFirstPage += offset + 1;
}
threads[forEnd] = new ParserThread(mReader, currentFirstPage, numPages);
threads[forEnd].start();
try
{
long before = System.currentTimeMillis();
for(int i = 0; i < numThreads; i++)
threads[i].join();
System.out.println(System.currentTimeMillis() - before);
}
catch(InterruptedException ie){ie.printStackTrace();}
mReader.close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numThreads;i++)
{
sb.append(threads[i].getText());
}
bodyText = sb.toString();
if (bodyText != "")
return bodyText;
else
return "error";
}
public static void main(final String args[]) throws IOException
{
for (int i = 0; i < args.length; i++)
{
String text = new PDFParser(args[i]).parse();
// System.out.println(text);
}
}
}
class ParserThread extends Thread
{
private PdfReader mReader;
private int mFirstPage;
private int mLastPage;
private String text;
ParserThread(PdfReader reader, int firstPage, int lastPage)
{
mReader = reader;
mFirstPage = firstPage;
mLastPage = lastPage;
text = "";
}
@Override
public void run()
{
StringBuilder sb = new StringBuilder();
try
{
for (int i = mFirstPage; i <= mLastPage; i++)
sb.append(PdfTextExtractor.getTextFromPage(mReader, i));
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
text = sb.toString();
}
public String getText()
{
return text;
}
}