我正在为几个函数添加内存。这些函数采用 2-3 个字符串参数(对象名称)、一个可选的 int 参数(记录 ID)和一个 bool 参数(包括已删除的记录)。每个参数组合都保证产生唯一的结果(因此值得缓存)。
我想知道连接给定的参数($param1 . $param2 . $param3 等)并将其用作数组键是否会更快,或者采用相同的连接字符串并使用 md5 哈希作为键。在 99% 的情况下,连接参数字符串的长度在 20-32 个字符之间(平均约为 27 个),而 md5 哈希始终为 32 个字符。
编辑 : 一个 md5 哈希只有 16 个字节,而不是 32 个。谢谢 Mjh。
我倾向于第一个选项,因为它:
# 分隔各个参数字符,它永远不会自然出现在任何参数中。crc32($paramString) .内存占用小,校验和计算功能非常快。key => value每个配对。 values所有 4 个数组中的一个是相同的。 keys也是相同的,除了对于前 2 个数组,连接的字符串键首先具有 crc32()跑到他们身上。$test1Array = [];
$start1 = microtime(true);
for ($i = 0; $i < 1000000; $i++)
{
$test1Array[crc32("pagemanagement" . "#" . "staticblocktype" . "#" . $i . "#" . 1)] = "test " . $i;
}
$end1 = microtime(true);
$test2Array = [];
$start2 = microtime(true);
for ($j = 0; $j < 1000000; $j++)
{
$test2Array[crc32("pagemanagement" . "#" . "staticblocktype" . "#" . $i . "#" . 1)] = "test " . $j;
}
$end2 = microtime(true);
$test3Array = [];
$start3 = microtime(true);
for ($x = 0; $x < 1000000; $x++)
{
$test3Array["pagemanagement" . "#" . "staticblocktype" . "#" . $i . "#" . 1] = "test " . $x;
}
$end3 = microtime(true);
$test4Array = [];
$start4 = microtime(true);
for ($y = 0; $y < 1000000; $y++)
{
$test4Array["pagemanagement" . "#" . "staticblocktype" . "#" . $i . "#" . 1] = "test " . $y;
}
$end4 = microtime(true);
crc32() value 保证比平均 27 个字符长度的字符串占用更少的内存。假设最好的情况是 1 字节字符,即每个缓存结果有 23 字节的差异。md5() 进行了快速测试还有:md5() 之间的性能差异如此之小感到惊讶和 crc32() .当然,crc32()仍然有优势,只使用4个字节到md5()是 16。最佳答案
这是我在使用 PHP7 的 AMD 2x2.3 GHz 机器上对 md5-crc32-sha1-native 散列的简单性能测试:
function probe($label, $times, $callback) {
$mem = memory_get_usage();
$start = microtime(true);
$array = $callback($times);
$time = microtime(true) - $start;
$mem = sprintf('%.3f', (memory_get_usage() - $mem) / 1024 / 1024);
return "$label: $time s, $mem MB";
}
$times = 1000000;
$run1 = probe('String key', $times, function ($times) {
$a = [];
while ($times-- > 0) {
$a["pagemanagement" . "#" . "staticblocktype" . "#" . $times . "#" . 1] = "test " . $times;
}
return $a;
});
$run2 = probe('CRC32 key', $times, function ($times) {
$a = [];
while ($times-- > 0) {
$a[crc32("pagemanagement" . "#" . "staticblocktype" . "#" . $times . "#" . 1)] = "test " . $times;
}
return $a;
});
$run3 = probe('MD5 key', $times, function ($times) {
$a = [];
while ($times-- > 0) {
$a[md5("pagemanagement" . "#" . "staticblocktype" . "#" . $times . "#" . 1)] = "test " . $times;
}
return $a;
});
$run4 = probe('SHA1 key', $times, function ($times) {
$a = [];
while ($times-- > 0) {
$a[sha1("pagemanagement" . "#" . "staticblocktype" . "#" . $times . "#" . 1)] = "test " . $times;
}
return $a;
});
echo join("<br/>\n", [
$run1,
$run2,
$run3,
$run4,
]);
String key: 1.2421879768372 s, 111.923 MB
CRC32 key: 1.3447260856628 s, 58.517 MB
MD5 key: 2.1748039722443 s, 111.923 MB
SHA1 key: 2.2480459213257 s, 119.552 MB
crc32在建议的测试集上占用的内存大约减少 1.5-2 倍。关于php - 内存:连接参数或执行 md5 哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36064690/
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat