在 BerkeleyDB 中多次打开数据库时性能损失

发布于 2024-08-04 18:07:28 字数 540 浏览 9 评论 0原文

我正在使用 BerkeleyDB 开发一个小型应用程序。我有一个关于在 BDB 中多次打开数据库的问题。

我有一大堆文本(语料库),我想加载其中的一部分来进行计算。我这里有两个伪代码(与python混合)

@1

def getCorpus(token):
    DB.open()
    DB.get(token)
    DB.close()

@2

#open and wait
def openCorpus():
    DB.open()

#close database
def closeCorpus():
    DB.close()

def getCorpus(token):
    DB.get(token)

在第二个示例中,我将在计算之前打开数据库,为每个循环加载令牌,然后关闭数据库。

在第一个示例中,每次循环请求令牌时,我都会打开、获取然后关闭数据库。

有性能损失吗?

我还注意到我正在使用 DBEnv 来管理数据库

I'm using BerkeleyDB to develop a small app. And I have a question about opening a database multiple time in BDB.

I have a large set of text ( corpus ), and I want to load a part of it to do the calculation. I have two pseudo-code (mix with python) here

@1

def getCorpus(token):
    DB.open()
    DB.get(token)
    DB.close()

@2

#open and wait
def openCorpus():
    DB.open()

#close database
def closeCorpus():
    DB.close()

def getCorpus(token):
    DB.get(token)

In the second example, I'll open the db before the calculation, load token for each loop and then close the db.

In the first example, each time the loop ask for the token, I'll open, get and then close the db.

Is there any performance lost ?

I also note that I'm using a DBEnv to manage the database

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2024-08-11 18:07:28

如果你不缓存打开的文件,你总是会损失性能,因为:

  • 你多次调用 open() 和 close() ,这是相当昂贵的,
  • 你会丢失所有潜在的缓冲区(系统缓冲区和 bdb 内部缓冲区)。

但在代码编写之前我不会太关心性能。

If you aren't caching the opened file you will always get performance lost because:

  • you call open() and close() multiple times which are quite expensive,
  • you lose all potential buffers (both system buffers and bdb internal buffers).

But I wouldn't care too much about the performance before the code is written.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文