溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

發(fā)布時(shí)間:2022-08-11 09:48:01 來(lái)源:億速云 閱讀:243 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮”吧!

一、通用算法的短字符壓縮

開(kāi)門(mén)見(jiàn)山,我們使用一段比較短的文本:Narrator: It is raining today. So, Peppa and George cannot  play outside.Peppa: Daddy, it's stopped raining.

使用ZSTD與LZ4分別壓縮一下上面這段短文本。下面分別是它們的壓縮結(jié)果。

ZSTD:

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

LZ4:

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

對(duì)短文本的壓縮,zstd的壓縮率很低,lz4壓縮后的文本長(zhǎng)度盡然超過(guò)了原有字符串的長(zhǎng)度。這是為什么?說(shuō)實(shí)話在這之前我也沒(méi)想到。

引用兩位大佬的名言:

Are you ok?  

What's your problem?

二、短字符串壓縮

從上面的結(jié)果可以得知,任何壓縮算法都有它的使用場(chǎng)景,并不是所有長(zhǎng)度的字符串都適合被某種算法壓縮。一般原因是通用壓縮算法維護(hù)了被壓縮字符串的,用于字符串還原的相關(guān)數(shù)據(jù)結(jié)構(gòu),而這些數(shù)據(jù)結(jié)構(gòu)的長(zhǎng)度超過(guò)了被壓縮短字符串的自身長(zhǎng)度。

那么問(wèn)題來(lái)了,“我真的有壓縮短字符串的需求,我想體驗(yàn)壓縮的極致感,怎么辦?”。

短字符壓縮算法它來(lái)了。這里挑選了3種比較優(yōu)異的短字符壓縮算法,分別是smaz,shoco,以及壓軸的unisox2。跟前兩章一樣,還是從壓縮率,壓縮和解壓縮性能的角度,一起看看他們?cè)诙套址麎嚎s場(chǎng)景的各自表現(xiàn)吧。

(1)Smaz

1、Smaz的壓縮和解壓縮

#include <stdio.h>
#include <string.h>
#include <iostream>
#include "smaz.h"

using namespace std;

int main()
{
    int buf_len;
    int com_size;
    int decom_size;

    char com_buf[4096] = {0};
    char decom_buf[4096] = {0};

    char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";

    buf_len = strlen(str_buf);
    com_size = smaz_compress(str_buf, buf_len, com_buf, 4096);

    cout << "text size:" << buf_len << endl;
    cout << "compress text size:" << com_size << endl;
    cout << "compress ratio:" << (float)buf_len / (float)com_size << endl << endl;

    decom_size = smaz_decompress(com_buf, com_size, decom_buf, 4096);
    cout << "decompress text size:" << decom_size << endl;

    if(strncmp(str_buf, decom_buf, buf_len)) {
        cout << "decompress text is not equal to source text" << endl;
    }

    return 0;
}

執(zhí)行結(jié)果如下:

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

通過(guò)smaz壓縮后的短字符串長(zhǎng)度為77,和源字符串相比,減少了30Byte。

2、Smaz的壓縮和解壓縮性能

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sys/time.h>
#include "smaz.h"

using namespace std;

int main()
{
    int cnt = 0;
    int buf_len;
    int com_size;
    int decom_size;

    timeval st, et;

    char *com_ptr = NULL;
    char* decom_ptr = NULL;

    char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";

    buf_len = strlen(str_buf);
    gettimeofday(&st, NULL);
    while(1) {

        com_ptr = (char *)malloc(buf_len);
        com_size = smaz_compress(str_buf, buf_len, com_ptr, buf_len);

        free(com_ptr);
        cnt++;

        gettimeofday(&et, NULL);
        if(et.tv_sec - st.tv_sec >= 10) {
            break;
        }
    }

    cout << endl <<"compress per second:" << cnt/10 << " times" << endl;

    cnt = 0;
    com_ptr = (char *)malloc(buf_len);
    com_size = smaz_compress(str_buf, buf_len, com_ptr, buf_len);

    gettimeofday(&st, NULL);
    while(1) {

        // decompress length not more than origin buf length
        decom_ptr = (char *)malloc(buf_len + 1);
        decom_size = smaz_decompress(com_ptr, com_size, decom_ptr, buf_len + 1);

        // check decompress length
        if(buf_len != decom_size) {
            cout << "decom error" << endl;
        }

        free(decom_ptr);
        cnt++;

        gettimeofday(&et, NULL);
        if(et.tv_sec - st.tv_sec >= 10) {
            break;
        }
    }

    cout << "decompress per second:" << cnt/10 << " times" << endl << endl;

    free(com_ptr);
    return 0;
}

結(jié)果如何?

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

壓縮性能在40w條/S,解壓在百萬(wàn)級(jí),好像還不錯(cuò)哈!

(2)Shoco

1、Shoco的壓縮和解壓縮

#include <stdio.h>
#include <string.h>
#include <iostream>
#include "shoco.h"

using namespace std;

int main()
{
    int buf_len;
    int com_size;
    int decom_size;

    char com_buf[4096] = {0};
    char decom_buf[4096] = {0};

    char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";

    buf_len = strlen(str_buf);
    com_size = shoco_compress(str_buf, buf_len, com_buf, 4096);

    cout << "text size:" << buf_len << endl;
    cout << "compress text size:" << com_size << endl;
    cout << "compress ratio:" << (float)buf_len / (float)com_size << endl << endl;

    decom_size = shoco_decompress(com_buf, com_size, decom_buf, 4096);
    cout << "decompress text size:" << decom_size << endl;

    if(strncmp(str_buf, decom_buf, buf_len)) {
        cout << "decompress text is not equal to source text" << endl;
    }

    return 0;
}

執(zhí)行結(jié)果如下:

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

通過(guò)shoco壓縮后的短字符串長(zhǎng)度為86,和源字符串相比,減少了21Byte。壓縮率比smaz要低。

 2、Shoco的壓縮和解壓縮性能

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sys/time.h>
#include "shoco.h"

using namespace std;

int main()
{
    int cnt = 0;
    int buf_len;
    int com_size;
    int decom_size;

    timeval st, et;

    char *com_ptr = NULL;
    char* decom_ptr = NULL;

    char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";

    buf_len = strlen(str_buf);
    gettimeofday(&st, NULL);
    while(1) {

        com_ptr = (char *)malloc(buf_len);
        com_size = shoco_compress(str_buf, buf_len, com_ptr, buf_len);

        free(com_ptr);
        cnt++;

        gettimeofday(&et, NULL);
        if(et.tv_sec - st.tv_sec >= 10) {
            break;
        }
    }

    cout << endl <<"compress per second:" << cnt/10 << " times" << endl;

    cnt = 0;
    com_ptr = (char *)malloc(buf_len);
    com_size = shoco_compress(str_buf, buf_len, com_ptr, buf_len);

    gettimeofday(&st, NULL);
    while(1) {

        // decompress length not more than origin buf length
        decom_ptr = (char *)malloc(buf_len + 1);
        decom_size = shoco_decompress(com_ptr, com_size, decom_ptr, buf_len + 1);

        // check decompress length
        if(buf_len != decom_size) {
            cout << "decom error" << endl;
        }

        free(decom_ptr);
        cnt++;

        gettimeofday(&et, NULL);
        if(et.tv_sec - st.tv_sec >= 10) {
            break;
        }
    }

    cout << "decompress per second:" << cnt/10 << " times" << endl << endl;

    free(com_ptr);
    return 0;
}

執(zhí)行結(jié)果如何呢?

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

holy shit!壓縮和解壓縮居然都達(dá)到了驚人的百萬(wàn)級(jí)。就像算法作者們自己說(shuō)的一樣:“在長(zhǎng)字符串壓縮領(lǐng)域,shoco不想與通用壓縮算法競(jìng)爭(zhēng),我們的優(yōu)勢(shì)是短字符的快速壓縮,雖然壓縮率很爛!”。這樣說(shuō),好像也沒(méi)毛病。

(3)Unisox2

我們?cè)賮?lái)看看unisox2呢。

1、Unisox2的壓縮和解壓縮

#include <stdio.h>
#include <string.h>
#include "unishox2.h"

int main()
{
    int buf_len;
    int com_size;
    int decom_size;

    char com_buf[4096] = {0};
    char decom_buf[4096] = {0};

    char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";

    buf_len = strlen(str_buf);
    com_size = unishox2_compress_simple(str_buf, buf_len, com_buf);

    printf("text size:%d\n", buf_len);
    printf("compress text size:%d\n", com_size);
    printf("compress ratio:%f\n\n", (float)buf_len / (float)com_size);

    decom_size = unishox2_decompress_simple(com_buf, com_size, decom_buf);

    printf("decompress text size:%d\n", decom_size);

    if(strncmp(str_buf, decom_buf, buf_len)) {
        printf("decompress text is not equal to source text\n");
    }

    return 0;
}

結(jié)果如下:

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

通過(guò)Unisox2壓縮后的短字符串長(zhǎng)度為67,和源字符串相比,減少了40Byte,相當(dāng)于是打了6折啊!不錯(cuò)不錯(cuò)。

 2、Unisox2的壓縮和解壓縮性能

Unisox2的壓縮能力目前來(lái)看是三者中最好的,如果他的壓縮和解壓性能也不錯(cuò)的話,那就真的就比較完美了。再一起看看Unisox2的壓縮和解壓性能吧!

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/time.h>
#include "unishox2.h"

int main()
{
    int cnt = 0;
    int buf_len;
    int com_size;
    int decom_size;

    struct timeval st, et;

    char *com_ptr = NULL;
    char* decom_ptr = NULL;

    char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";

    buf_len = strlen(str_buf);
    gettimeofday(&st, NULL);
    while(1) {

        com_ptr = (char *)malloc(buf_len);
        com_size = unishox2_compress_simple(str_buf, buf_len, com_ptr);

        free(com_ptr);
        cnt++;

        gettimeofday(&et, NULL);
        if(et.tv_sec - st.tv_sec >= 10) {
            break;
        }
    }

    printf("\ncompress per second:%d times\n", cnt/10);

    cnt = 0;
    com_ptr = (char *)malloc(buf_len);
    com_size = unishox2_compress_simple(str_buf, buf_len, com_ptr);

    gettimeofday(&st, NULL);
    while(1) {

        // decompress length not more than origin buf length
        decom_ptr = (char *)malloc(buf_len + 1);
        decom_size = unishox2_decompress_simple(com_ptr, com_size, decom_ptr);

        // check decompress length
        if(buf_len != decom_size) {
            printf("decom error\n");
        }

        free(decom_ptr);
        cnt++;

        gettimeofday(&et, NULL);
        if(et.tv_sec - st.tv_sec >= 10) {
            break;
        }
    }

    printf("decompress per second:%d times\n\n", cnt/10);

    free(com_ptr);
    return 0;
}

執(zhí)行結(jié)果如下:

C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮

事與愿違,Unisox2雖然有三個(gè)算法中最好的壓縮率,可是卻也擁有最差的壓縮和解壓性能。

感謝各位的閱讀,以上就是“C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C語(yǔ)言如何實(shí)現(xiàn)短字符串壓縮這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI