{ yeah : 必须哒 } No place to place should record our youth?

10Feb/110

【转】设置自动重连的ssh代理通道

Posted by alacner

我目前常用的翻墙办法就是拿ssh搭个代理通道,然后chrome + switch!插件一起配合,这就算翻墙了。这法子只要拿个机器跑一小脚本,比如:

ssh -D 7070 -qnN [username]@[server]

但是ssh通道如果闲置了一段时间,就会自动断连,等我需要用到代理的时候往往又得蛋疼的重新跑一遍,非常麻烦。所以我刻苦学习前辈的经验,找到一个解决办法,在mac或linux下都可使用,分享如下:

  • 把ssh配置为免密码登录,这个一搜一大把,略过不提
  • 在/etc/inittab的最后一行加上:

    tunl:345:respawn:/usr/bin/ssh -D 7070 -qnN [username]@[server] > /dev/null 2>&1

  • 让修改的inittab马上生效

    sudo init q

然后这个ssh通道就会自动重连了。

【转自:Volcano】

Filed under: 技术日志 No Comments
10Feb/110

[转]PHP数组交集的优化

Posted by alacner

假设我们正在运营一个手机相关的网站,用户可以通过指定若干参数(如操作系统,屏幕分辨率,摄像头像素等等)来筛选自己想要的手机。不过由于手机的 参数多,且不同的手机其参数差异大,所以参数表结构通常是纵表(一个参数是一行),而不是横表(一个参数是一列),此时使用若干参数来取结果,通常就是把 每个单独参数来取结果,再一起取交集。

假定每个参数会包含一千个左右的产品ID(int),以此为前提来模拟生成一些数据:

<?php
$rand = function() {
 $result = array();
 for ($i = 0; $i < 1000; $i++) {
 $result[] = mt_rand(1, 10000);
 }
 return $result;
};
$param_a = $rand();
$param_b = $rand();
?>

注意:如果测试数据集过小的话,结论可能会出现不一致。

先看看通过PHP内置方法array_intersect实现的性能:

<?php
$time = microtime(true);
$result = array_intersect($param_a, $param_b);
$time = microtime(true) - $time;
echo "array_intersect: {$time}n";
?>


在优化之前,我们先来看看array_intersect一些特殊的地方:

</pre>
<span style="font-family: 宋体; font-size: 12pt;"><?php</span>

<span style="font-family: 宋体; font-size: 12pt;">$param_a = array(1, 2, 2);</span>

<span style="font-family: 宋体; font-size: 12pt;">$param_b = array(1, 2, 3);</span>

<span style="font-family: 宋体; font-size: 12pt;">var_dump(</span>

<span style="font-family: 宋体; font-size: 12pt;"> array_intersect($param_a, $param_b),</span>

<span style="font-family: 宋体; font-size: 12pt;"> array_intersect($param_b, $param_a)</span>

<span style="font-family: 宋体; font-size: 12pt;">);</span>

<span style="font-family: 宋体; font-size: 12pt;">?></span>

<span style="font-family: 宋体; font-size: 12pt;">

  • array_intersect($param_a, $param_b): 1, 2, 2

  • array_intersect($param_b, $param_a): 1, 2

也就是说,如果在第一个数组参数中有重复元素的话,则array_intersect会返回所有满足条件的重复元素。改写array_intersect的时候最好兼容这些功能。

下面看看通过自定义方法int_array_intersect实现的性能:

<?php

function int_array_intersect()

{

if (func_num_args() < 2) {

trigger_error('param error', E_USER_ERROR);

}

$args = func_get_args();

foreach ($args AS $arg) {

if (!is_array($arg)) {

trigger_error('param error', E_USER_ERROR);

}

}

$intersect = function($a, $b) {

$result = array();

$length_a = count($a);

$length_b = count($b);

for ($i = 0, $j = 0; $i < $length_a && $j < $length_b; null) {

if($a[$i] < $b[$j] && ++$i) {

continue;

}

if($a[$i] > $b[$j] && ++$j) {

continue;

}

$result[] = $a[$i];

if (isset($a[$next = $i + 1]) && $a[$next] != $a[$i]) {

++$j;

}

++$i;

}

return $result;

};

$result = array_shift($args);

sort($result);

foreach ($args as $arg) {

sort($arg);

$result = $intersect($result, $arg);

}

return $result;

}

$time = microtime(true);

$result = int_array_intersect($param_a, $param_b);

$time = microtime(true) - $time;

echo "int_array_intersect: {$time}n";

?>

直觉上,我们肯定会认为内置函数快于自定义函数,但本例中结果恰恰相反:

  • array_intersect: 0.023918151855469

  • int_array_intersect: 0.0026049613952637

为什么?原因在于int_array_intersect操作的都是整数,而array_intersect操作的都是字符串,即便你传给它整数,也会当做字符串处理。

注:测试结果基于PHP5.3.5,不同版本结论可能存在差异

参考:Faster array_intersect

【转自:http://huoding.com/2011/01/30/43】

I've written a set-intersection implementation which is MUCH (5+ times) faster than array_intersect()

PHP Code:

function intersect($s1,$s2) {

$i=0;

$j=0;

$N count($s1);

$M count($s2);

$intersection = array();

while($i<$N && $j<$M) {

if($s1[$i]<$s2[$j]) $i++;

else if($s1[$i]>$s2[$j]) $j++;

else {

$intersection[] = $s1[$i];

$i++;

$j++;

}

}

return $intersection;

}

Both arrays must be pre-sorted, but I did the performance comparison including this time, just to be fair .

Just thought I'd share this one.

I should probably have been clearer about the differences. In my case I had suitable arrays, and need the speed more than the generality. Its is definately significantly faster for my data at any rate.

Perhaps I'll take a look at the array_intersect source at some point.

Thanks for the comments guys

Filed under: 技术日志 No Comments