The use of Gperf

Source: Internet
Author: User

Recently has been doing string hash lookup, someone recommended the use of Gperf perfect hash, so to investigate the use of Gperf methods.

Gperf download Http://,ubuntu can be directly sudo apt-get install GPERF, the possible way is not up to date.

Gperf Document Http://

I mainly refer to the "use of gperf to achieve efficient C + + command line processing."

Gperf use:

Mainly the declarations and keyword parts, if simply find the existence of a string, you do not need the Declarations section,. gperf file directly save all the strings in the hash table. However, the hash table is usually the key and value pairs, such as in the declaration part of the Declaration of a struct, and then two percent after the keyword. The format is as follows:

struct KeyValue
    const char * key;//The first field must be a const char *
    int  value;      //can have more value  

Keyword Part if the first character is%, the string must be enclosed in double quotes, such as%key, which is "%key".

Then use the Gperf command to generate the HPP file:

Gperf-cgd-k key-l C + +-T xxx.gperf > PERFECTHASH.HPP

The generated header file has Perfect_hash classes, and the class name can be modified with the-Z option.

Class has a hash function, the default is hash (), can be modified through-h option.

The lookup function is In_word_set (), modified with-N.

-l Specifies the programming language.

If you have a declaration section, you must use the-t option.

-k Specifies the name of the keyword in struct.

The concrete principle does not make the detailed explanation, the Perfect hash should be quick, has not actually tested.


Gperf just put the keywords in the. gperf file in the form of a structure array in the code file, eliminating the time to load the file, but feel that this approach is only suitable for small-scale hash lookups, such as C + + command line processing in the reference. For the hundreds of thousands of or millions of string index pairs I am dealing with, it is not appropriate to use GPERF for processing.

Do not know whether someone has handled a large string hash lookup, whether there is a better solution, please enlighten me, thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.