开发者

size (in KB) of variable in php

开发者 https://www.devze.com 2023-04-05 06:35 出处:网络
is it possible to calculate / estimate the size (in KB) of a variable (string, array but mostly array). What happen is that we store some data inside memcache开发者_StackOverflow社区, and we would lik

is it possible to calculate / estimate the size (in KB) of a variable (string, array but mostly array). What happen is that we store some data inside memcache开发者_StackOverflow社区, and we would like to know how much memory space that data would take inside memcache.


I believe PHP's memcache implementation uses serialize when storing in memcached. You can simply serialize the output and check it's size:

<?php
$data = array('foo' => 'bar');
$serialized_data = serialize($data);
$size = strlen($serialized_data);
# `strlen` returns number of chars in a string. Each char is 1 byte.
# So to get size in bits, multiply `strlen` results by 8. Divide by
# 1024 for KB or KiB. Divide by 1000 for kB.
print($size * 8 / 1000);
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消