knightrider
knightrider

Reputation: 2143

processing in php vs c++

I need to design a function which uses hashtable. It basically inserts data into hashtable and search for items. Typically the function will take 15sec to 10min for execution. Should I implement this function in c++ and use a system call in PHP or should I implement it in php using associative arrays. Which would be more efficient. What are the advantage and disadvantage of each.

The key will be a string. The value will be one structure which contains two other structures. The first structure basically contains an array of integers and the second will contain an array of integer pair values

Upvotes: 3

Views: 926

Answers (2)

Peter Krejci
Peter Krejci

Reputation: 3192

PHP is well known for its fast associative array implementation, but according to my experiences, C++ is still faster. A few months ago I needed to implement fast prefix matching, there were thousands of prefixes in hash table and millions of strings to be matched. I made both, PHP and C++ implementations, and as I remember C++ was more than 10 times faster and consumed much less memory. But of course, it heavily depends also on your algorithm, not only on hash table implementation.

Upvotes: 1

ThomasMcLeod
ThomasMcLeod

Reputation: 7769

Apparently, PHP arrays are implemented as a linked hash table. See How is the PHP array implemented on the C level?.

In any case, for 300 items there would probably be little speed difference in the type of container you used. I would stay in PHP if possible for simplicity.

Upvotes: 1

Related Questions