<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      字符串壓縮(三)之短字符串壓縮

        本文來自博客園,作者:T-BARBARIANS,博文嚴禁轉載,轉載必究!

       

       前言

        上一篇探索了LZ4的壓縮和解壓性能,以及對LZ4和ZSTD的壓縮、解壓性能進行了橫向對比。文末的最后也給了一個彩蛋:任意長度的字符串都可以被ZSTD、LZ4之類的壓縮算法壓縮得很好嗎?

        本篇我們就來一探究竟。

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

        開門見山,我們使用一段比較短的文本:Narrator: It is raining today. So, Peppa and George cannot  play outside.Peppa: Daddy, it's stopped raining.

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

        ZSTD:

        

         LZ4:

        

        對短文本的壓縮,zstd的壓縮率很低,lz4壓縮后的文本長度盡然超過了原有字符串的長度。這是為什么?說實話在這之前我也沒想到。

        引用兩位大佬的名言:

        Are you ok?  

        What's your problem?

      二、短字符串壓縮

        從上面的結果可以得知,任何壓縮算法都有它的使用場景,并不是所有長度的字符串都適合被某種算法壓縮。一般原因是通用壓縮算法維護了被壓縮字符串的,用于字符串還原的相關數據結構,而這些數據結構的長度超過了被壓縮短字符串的自身長度。

        那么問題來了,“我真的有壓縮短字符串的需求,我想體驗壓縮的極致感,怎么辦?”。

        短字符壓縮算法它來了。這里挑選了3種比較優異的短字符壓縮算法,分別是smaz,shoco,以及壓軸的unisox2。跟前兩章一樣,還是從壓縮率,壓縮和解壓縮性能的角度,一起看看他們在短字符壓縮場景的各自表現吧。

      (1)Smaz

      1、Smaz的壓縮和解壓縮

       1 #include <stdio.h>
       2 #include <string.h>
       3 #include <iostream>
       4 #include "smaz.h"
       5 
       6 using namespace std;
       7 
       8 int main()
       9 {
      10     int buf_len;
      11     int com_size;
      12     int decom_size;
      13     
      14     char com_buf[4096] = {0};
      15     char decom_buf[4096] = {0};
      16 
      17     char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";
      18 
      19     buf_len = strlen(str_buf);
      20     com_size = smaz_compress(str_buf, buf_len, com_buf, 4096);
      21     
      22     cout << "text size:" << buf_len << endl;
      23     cout << "compress text size:" << com_size << endl;
      24     cout << "compress ratio:" << (float)buf_len / (float)com_size << endl << endl;
      25 
      26     decom_size = smaz_decompress(com_buf, com_size, decom_buf, 4096);
      27     cout << "decompress text size:" << decom_size << endl;
      28 
      29     if(strncmp(str_buf, decom_buf, buf_len)) {
      30         cout << "decompress text is not equal to source text" << endl;
      31     }
      32 
      33     return 0;
      34 }

        執行結果如下:

         

         通過smaz壓縮后的短字符串長度為77,和源字符串相比,減少了30Byte。

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

       1 #include <stdio.h>
       2 #include <string.h>
       3 #include <iostream>
       4 #include <sys/time.h>
       5 #include "smaz.h"
       6 
       7 using namespace std;
       8 
       9 int main()
      10 {
      11     int cnt = 0;
      12     int buf_len;
      13     int com_size;
      14     int decom_size;
      15 
      16     timeval st, et;
      17     
      18     char *com_ptr = NULL;
      19     char* decom_ptr = NULL;
      20 
      21     char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";
      22 
      23     buf_len = strlen(str_buf);
      24     gettimeofday(&st, NULL);
      25     while(1) {
      26         
      27         com_ptr = (char *)malloc(buf_len);
      28         com_size = smaz_compress(str_buf, buf_len, com_ptr, buf_len);
      29 
      30         free(com_ptr);
      31         cnt++;
      32         
      33         gettimeofday(&et, NULL);
      34         if(et.tv_sec - st.tv_sec >= 10) {
      35             break;
      36         }
      37     }
      38 
      39     cout << endl <<"compress per second:" << cnt/10 << " times" << endl;
      40 
      41     cnt = 0;
      42     com_ptr = (char *)malloc(buf_len);
      43     com_size = smaz_compress(str_buf, buf_len, com_ptr, buf_len);
      44     
      45     gettimeofday(&st, NULL);
      46     while(1) {
      47 
      48         // decompress length not more than origin buf length
      49         decom_ptr = (char *)malloc(buf_len + 1);
      50         decom_size = smaz_decompress(com_ptr, com_size, decom_ptr, buf_len + 1);
      51 
      52         // check decompress length
      53         if(buf_len != decom_size) {
      54             cout << "decom error" << endl;
      55         }
      56         
      57         free(decom_ptr);
      58         cnt++;
      59         
      60         gettimeofday(&et, NULL);
      61         if(et.tv_sec - st.tv_sec >= 10) {
      62             break;
      63         }
      64     }
      65 
      66     cout << "decompress per second:" << cnt/10 << " times" << endl << endl;
      67     
      68     free(com_ptr);
      69     return 0;
      70 }

        結果如何?

        

         壓縮性能在40w條/S,解壓在百萬級,好像還不錯哈!

      (2)Shoco

      1、Shoco的壓縮和解壓縮

       1 #include <stdio.h>
       2 #include <string.h>
       3 #include <iostream>
       4 #include "shoco.h"
       5 
       6 using namespace std;
       7 
       8 int main()
       9 {
      10     int buf_len;
      11     int com_size;
      12     int decom_size;
      13     
      14     char com_buf[4096] = {0};
      15     char decom_buf[4096] = {0};
      16 
      17     char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";
      18 
      19     buf_len = strlen(str_buf);
      20     com_size = shoco_compress(str_buf, buf_len, com_buf, 4096);
      21     
      22     cout << "text size:" << buf_len << endl;
      23     cout << "compress text size:" << com_size << endl;
      24     cout << "compress ratio:" << (float)buf_len / (float)com_size << endl << endl;
      25 
      26     decom_size = shoco_decompress(com_buf, com_size, decom_buf, 4096);
      27     cout << "decompress text size:" << decom_size << endl;
      28 
      29     if(strncmp(str_buf, decom_buf, buf_len)) {
      30         cout << "decompress text is not equal to source text" << endl;
      31     }
      32 
      33     return 0;
      34 }

        執行結果如下:

        

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

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

       1 #include <stdio.h>
       2 #include <string.h>
       3 #include <iostream>
       4 #include <sys/time.h>
       5 #include "shoco.h"
       6 
       7 using namespace std;
       8 
       9 int main()
      10 {
      11     int cnt = 0;
      12     int buf_len;
      13     int com_size;
      14     int decom_size;
      15 
      16     timeval st, et;
      17     
      18     char *com_ptr = NULL;
      19     char* decom_ptr = NULL;
      20 
      21     char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";
      22 
      23     buf_len = strlen(str_buf);
      24     gettimeofday(&st, NULL);
      25     while(1) {
      26         
      27         com_ptr = (char *)malloc(buf_len);
      28         com_size = shoco_compress(str_buf, buf_len, com_ptr, buf_len);
      29 
      30         free(com_ptr);
      31         cnt++;
      32         
      33         gettimeofday(&et, NULL);
      34         if(et.tv_sec - st.tv_sec >= 10) {
      35             break;
      36         }
      37     }
      38 
      39     cout << endl <<"compress per second:" << cnt/10 << " times" << endl;
      40 
      41     cnt = 0;
      42     com_ptr = (char *)malloc(buf_len);
      43     com_size = shoco_compress(str_buf, buf_len, com_ptr, buf_len);
      44     
      45     gettimeofday(&st, NULL);
      46     while(1) {
      47 
      48         // decompress length not more than origin buf length
      49         decom_ptr = (char *)malloc(buf_len + 1);
      50         decom_size = shoco_decompress(com_ptr, com_size, decom_ptr, buf_len + 1);
      51 
      52         // check decompress length
      53         if(buf_len != decom_size) {
      54             cout << "decom error" << endl;
      55         }
      56         
      57         free(decom_ptr);
      58         cnt++;
      59         
      60         gettimeofday(&et, NULL);
      61         if(et.tv_sec - st.tv_sec >= 10) {
      62             break;
      63         }
      64     }
      65 
      66     cout << "decompress per second:" << cnt/10 << " times" << endl << endl;
      67     
      68     free(com_ptr);
      69     return 0;
      70 }

        執行結果如何呢?

        

         holy shit!壓縮和解壓縮居然都達到了驚人的百萬級。就像算法作者們自己說的一樣:“在長字符串壓縮領域,shoco不想與通用壓縮算法競爭,我們的優勢是短字符的快速壓縮,雖然壓縮率很爛!”。這樣說,好像也沒毛病。

      (3)Unisox2

        我們再來看看unisox2呢。

      1、Unisox2的壓縮和解壓縮

       1 #include <stdio.h>
       2 #include <string.h>
       3 #include "unishox2.h"
       4 
       5 int main()
       6 {
       7     int buf_len;
       8     int com_size;
       9     int decom_size;
      10     
      11     char com_buf[4096] = {0};
      12     char decom_buf[4096] = {0};
      13     
      14     char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";
      15     
      16     buf_len = strlen(str_buf);
      17     com_size = unishox2_compress_simple(str_buf, buf_len, com_buf);
      18 
      19     printf("text size:%d\n", buf_len);
      20     printf("compress text size:%d\n", com_size);
      21     printf("compress ratio:%f\n\n", (float)buf_len / (float)com_size);
      22     
      23     decom_size = unishox2_decompress_simple(com_buf, com_size, decom_buf);
      24 
      25     printf("decompress text size:%d\n", decom_size);
      26 
      27     if(strncmp(str_buf, decom_buf, buf_len)) {
      28         printf("decompress text is not equal to source text\n");
      29     }
      30 
      31     return 0;
      32 }

        結果如下:

        

         通過Unisox2壓縮后的短字符串長度為67,和源字符串相比,減少了40Byte,相當于是打了6折啊!不錯不錯。

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

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

       1 #include <stdio.h>
       2 #include <string.h>
       3 #include <malloc.h>
       4 #include <sys/time.h>
       5 #include "unishox2.h"
       6 
       7 int main()
       8 {
       9     int cnt = 0;
      10     int buf_len;
      11     int com_size;
      12     int decom_size;
      13 
      14     struct timeval st, et;
      15     
      16     char *com_ptr = NULL;
      17     char* decom_ptr = NULL;
      18 
      19     char str_buf[1024] = "Narrator: It is raining today. So, Peppa and George cannot play outside.Peppa: Daddy, it's stopped raining.";
      20 
      21     buf_len = strlen(str_buf);
      22     gettimeofday(&st, NULL);
      23     while(1) {
      24         
      25         com_ptr = (char *)malloc(buf_len);
      26         com_size = unishox2_compress_simple(str_buf, buf_len, com_ptr);
      27 
      28         free(com_ptr);
      29         cnt++;
      30         
      31         gettimeofday(&et, NULL);
      32         if(et.tv_sec - st.tv_sec >= 10) {
      33             break;
      34         }
      35     }
      36 
      37     printf("\ncompress per second:%d times\n", cnt/10);
      38 
      39     cnt = 0;
      40     com_ptr = (char *)malloc(buf_len);
      41     com_size = unishox2_compress_simple(str_buf, buf_len, com_ptr);
      42     
      43     gettimeofday(&st, NULL);
      44     while(1) {
      45 
      46         // decompress length not more than origin buf length
      47         decom_ptr = (char *)malloc(buf_len + 1);
      48         decom_size = unishox2_decompress_simple(com_ptr, com_size, decom_ptr);
      49 
      50         // check decompress length
      51         if(buf_len != decom_size) {
      52             printf("decom error\n");
      53         }
      54         
      55         free(decom_ptr);
      56         cnt++;
      57         
      58         gettimeofday(&et, NULL);
      59         if(et.tv_sec - st.tv_sec >= 10) {
      60             break;
      61         }
      62     }
      63 
      64     printf("decompress per second:%d times\n\n", cnt/10);
      65     
      66     free(com_ptr);
      67     return 0;
      68 }

        執行結果如下:

        

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

      三、總結

        本篇分享了smaz,shoco,unisox2三種短字符串壓縮算法,分別探索了它們各自的壓縮率與壓縮和解壓縮性能,結果如下表所示。

      表1

        shoco的壓縮率最低,但是擁有最高的壓縮和解壓速率;smaz居中;unisox2擁有最高的壓縮率,可是它的壓縮和解壓性能最低。

        結論與前兩章有關長字符串壓縮的分析不謀而合:擁有高壓縮率,就會損失自身的壓縮性能,兩者不可兼得。

        實際使用還是看自身需求和環境吧。如果適當壓縮就好,那就可以選用shoco,畢竟性能高;想要節約更多的空間,那就選擇smaz或者unisox2。

        好了,字符串壓縮系列的分享就到此為止了,如果對你有些許幫助,還請各位技術愛好者登錄點贊呀,謝謝!

       

        本文來自博客園,作者:T-BARBARIANS,博文嚴禁轉載,轉載必究!

      posted @ 2022-08-10 15:14  T-BARBARIANS  閱讀(5266)  評論(2)    收藏  舉報
      主站蜘蛛池模板: 粉嫩jk制服美女啪啪| 精品三级在线| 国产乱码精品一区二三区| 国产精品久久无码一区| 国产一级黄色片在线播放| 亚洲日韩国产中文其他| 九九热免费精品视频在线| 成人国产精品中文字幕| 久久这里有精品国产电影网 | 无码日韩精品一区二区人妻| 国产偷倩视频| 久久中文字幕国产精品| 18禁精品一区二区三区| 久久av无码精品人妻出轨| 色综合AV综合无码综合网站| 草草线在成年免费视频2| 亚洲av成人无码精品电影在线| 国产无遮挡又黄又大又爽| 实拍女处破www免费看| 亚洲人成人网站色www| 国产一区二区在线观看粉嫩| 婷婷色综合成人成人网小说 | 九九热在线视频只有精品| 中文字幕精品亚洲字幕成| 国产区成人精品视频| 欧美人妻一区二区三区| 久久综合综合久久综合| 亚洲天堂av日韩精品| 亚洲精品第一区二区三区| 久久久久无码国产精品不卡| 999精品全免费观看视频| 男女真人国产牲交a做片野外| 亚洲AV日韩精品久久久久| 国产嫩草精品网亚洲av| 亚洲国产精品嫩草影院久久| 亚洲春色在线视频| 18禁亚洲一区二区三区| 中文日产幕无线码一区中文| 国产精品69人妻我爱绿帽子| 国产AV福利第一精品| 亚洲精品一区二区妖精|