如何在C中高效实现PHP字符串索引数组?

发布于 2024-12-14 06:50:59 字数 570 浏览 0 评论 0原文

我一直在尝试在大列表中搜索文本,发现使用 PHP 数组似乎是一种快速的方法。

例如,如果您有大量地名和相关的邮政编码,您可以将它们读入 PHP 数组,如下所示:

$place[‘place name here’] = “postcode”;

然后要查找,您只需将要查找的位置并将其插入到数组中即可:

$postcode_sought = $place[‘place I want to look up’];

我想我可以加快速度使用C++,但当然C++不允许(据我所知)以字符串作为索引的数组。

我能想到的唯一方法是为地点和邮政编码创建向量,并循环遍历地点向量以查找匹配项,但重复的字符串比较需要永远进行,正如我所预期的那样。我还尝试过对文本进行哈希处理,但仍然无法像 PHP 那样快地得到它。

我认为 PHP 是用 C 编写的,所以我的问题是 C 如何设法为 PHP 创建这个字符串索引名称功能? 我不是在寻找实际的代码或任何东西,在我看来,必须有一些用于此目的的基本技术,我只是想知道是否有人可以简要解释它。

提前致谢。 C

I’ve been playing around with searching text in big lists and found that using a PHP array seems to be a quick way of doing it.

E.g. if you had loads of place names and associated postcodes you could read them into a PHP array like this:

$place[‘place name here’] = “postcode”;

Then to look up you just take the place you want to look up and plug it in to the array:

$postcode_sought = $place[‘place I want to look up’];

I thought I could speed this up using C++ but of course C++ does not allow (as far as I know) arrays with a string as the index.

The only way I can think to do it is to create vectors for the place and postcode and loop through the place vector looking for a match but the repeated string comparisons take forever as I'd expected. I also experimented with hashing the text but I still couldn’t get it anywhere near as fast as PHP.

I think PHP is written in C so my question is how does C manage to create this string index name functionality for PHP?
I’m not looking for the actual code or anything, it just seems to me that there must be some fundamental technique that is used for this and I was just wondering if there is anyone out there who could briefly explain it.

Thanks in advance.
C

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

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

发布评论

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

评论(3

许久 2024-12-21 06:50:59

我认为我可以使用 C++ 来加快速度,但当然 C++ 不允许(据我所知)以字符串作为索引的数组。

确实如此,您可以使用 std::map 作为关联数组。

I thought I could speed this up using C++ but of course C++ does not allow (as far as I know) arrays with a string as the index.

It does, You can use std::map as an Associative array.

浅浅淡淡 2024-12-21 06:50:59

您可以尝试使用 Berkeley DB。在过去,它是最快的,但默认情况下它是面向磁盘的。我不知道你是否可以在内存中运行它,但你总是可以从 tmpfs 挂载该目录。

PHP 可能会使用一些外部类作为哈希表。通过编写快速搜索算法,您可以走得更远。将钥匙排序并检查中间的钥匙。然后再次在中间,直到找到钥匙。您还可以使用 MD5() 作为键,因为它比纯字符串比较更快。

You could try using Berkeley DB. Back in the days it was the fastest but by default it's disk oriented. I don't know if you can run it in memory but you can always mount the directory from tmpfs.

PHP propably uses some external class for hashing table. You can get quite far by writing a quicksearch algorithm. Sort the keys and check up the key in the middle. Then again in middle until you've found the key. You can also use MD5() for keys as it's faster than pure string comparison.

乱了心跳 2024-12-21 06:50:59

C 和 C++ 只允许整数类型作为数组索引,而字符串甚至不是 C/C++ 上的类型,它们实际上是字符数组。
如上所述,使用 std::map 或类似的。

C and C++ only allow integer types to be array indexes, and strings aren't even a type on C/C++, they're actually an array of chars.
As stated above, use std::map or similar.

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