{"id":173,"date":"2012-07-25T10:43:30","date_gmt":"2012-07-25T09:43:30","guid":{"rendered":"http:\/\/pjsmith.me.uk\/?p=173"},"modified":"2012-07-25T10:59:05","modified_gmt":"2012-07-25T09:59:05","slug":"can-i-save-space-gzip-compressing-a-string-before-storage","status":"publish","type":"post","link":"https:\/\/pjsmith.me.uk\/index.php\/2012\/07\/can-i-save-space-gzip-compressing-a-string-before-storage\/","title":{"rendered":"Can I save space GZip Compressing a string before storage?"},"content":{"rendered":"<p>Ever wonder if it would be more efficient to save compressed versions of your strings for storage in databases etc?  I wanted to check several variations on this recently, so came up with the following simple test.  Just enter the string in the box below, select the compression level, and the answer will be shown. The php function &#8216;gzcompress&#8217; is used.<\/p>\n<form name=\"form1\" method=\"post\" action=\"\">\n<p>String to compress<br \/>\n    <label for=\"texttocompress\"><\/label><br \/>\n    <textarea name=\"texttocompress\" id=\"texttocompress\" cols=\"45\" rows=\"5\"><\/textarea>\n  <\/p>\n<p>Compression level, 0 = none, 9 = best (takes longer though)<br \/>\n    <label for=\"compressionlevel\"><\/label><br \/>\n    <select name=\"compressionlevel\" id=\"compressionlevel\"><option value=\"0\">0<\/option><option value=\"1\">1<\/option><option value=\"2\">2<\/option><option value=\"3\">3<\/option><option value=\"4\">4<\/option><option value=\"5\">5<\/option><option value=\"6\">6<\/option><option value=\"7\">7<\/option><option value=\"8\">8<\/option><option value=\"9\" selected=\"selected\">9<\/option><\/select><br \/>\n    <input type=\"submit\" name=\"Test it\" id=\"Test it\" value=\"Submit\">\n  <\/p>\n<\/form>\n<p>  <?php\n\tif (isset($_POST['texttocompress'])) :\n\t\n\t$time_start = microtime(true);\n\t$compressedstring = gzcompress($_POST['texttocompress'],$_POST['compressionlevel']);\n\t$time_end = microtime(true);\n\t$time = $time_end - $time_start;\n\t\n\t$uncompressed = strlen($_POST['texttocompress']);\n\t$compressed = strlen($compressedstring);\n\t\n\t$difference = number_format((($uncompressed - $compressed) \/ $uncompressed) * 100,2);\n\t\n?><\/p>\n<p><strong>The results of last text where as follows<\/strong><\/p>\n<p>The string was originally <?php echo $uncompressed;?> characters in length. The new compressed string is  <?php echo $compressed;?> characters in length. This is a saving of <?php echo $difference;?> percent. The conversion took <?php echo $time;?> microseconds. Compression level <?php echo $_POST['compressionlevel'];?> was used.<\/p>\n<p>Your original string<br \/>\n  <label for=\"textarea\"><\/label><br \/>\n  <textarea name=\"textarea\" id=\"textarea\" cols=\"45\" rows=\"5\"><?php echo $_POST['texttocompress'];?><br \/>\n  <\/textarea>\n<\/p>\n<p>Your compressed string (base64 encoded)<br \/>\n  <label for=\"textarea2\"><\/label><br \/>\n  <textarea name=\"textarea2\" id=\"textarea2\" cols=\"45\" rows=\"5\"><?php echo base64_encode($compressedstring);?><br \/>\n  <\/textarea>\n<\/p>\n<p>\n  <?php\t\tendif;\t?>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wonder if it would be more efficient to save compressed versions of your strings for storage in databases etc? I wanted to check several variations on this recently, so&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-173","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-tech"},"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":0,"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"wp:attachment":[{"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pjsmith.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}