本文介绍了php-帮助提高此youtube样式url生成器的效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一番搜索,我发现这个带有加密功能的youtube样式url生成器可以隐藏原始ID ...但是我希望提高效率,因为它将被大量使用.到目前为止,我已经将其提高了20%...谁能帮助我进一步提高它.

After some searching I found this youtube style url generator with encryption to hide the original id... however I am hoping to improve the efficiency as it will be used a lot. So far I have improved it by 20%... can anyone help me improve it more.

这是原始的:

function alphaID($in, $to_num = false, $pad_up = false, $passKey = null)
{
    $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if ($passKey !== null) {
        // Although this function's purpose is to just make the
        // ID short - and not so much secure,
        // with this patch by Simon Franz (http://blog.snaky.org/)
        // you can optionally supply a password to make it harder
        // to calculate the corresponding numeric ID

        for ($n = 0; $n<strlen($index); $n++) {
            $i[] = substr( $index,$n ,1);
        }

        $passhash = hash('sha256',$passKey);
        $passhash = (strlen($passhash) < strlen($index))
            ? hash('sha512',$passKey)
            : $passhash;

        for ($n=0; $n < strlen($index); $n++) {
            $p[] = substr($passhash, $n ,1);
        }

        array_multisort($p, SORT_DESC, $i);
        $index = implode($i);
    }

    $base = strlen($index);

    if ($to_num) {
        // Digital number <<-- alphabet letter code
        $in = strrev($in);
        $out = 0;
        $len = strlen($in) - 1;
        for ($t = 0; $t <= $len; $t++) {
            $bcpow = bcpow($base, $len - $t);
            $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
        }

        if (is_numeric($pad_up)) {
            $pad_up--;
            if ($pad_up > 0) {
                $out -= pow($base, $pad_up);
            }
        }
    } else {
        // Digital number -->> alphabet letter code
        if (is_numeric($pad_up)) {
            $pad_up--;
            if ($pad_up > 0) {
                $in += pow($base, $pad_up);
            }
        }

        $out = "";
        for ($t = floor(log10($in) / log10($base)); $t >= 0; $t--) {
            $a = floor($in / bcpow($base, $t));
            $out = $out . substr($index, $a, 1);
            $in = $in - ($a * bcpow($base, $t));
        }
        $out = strrev($out); // reverse
    }

    return $out;
}

到目前为止,这是我修改后的代码:

Here is my modified code so far:

function alphaID($in, $to_num = false, $pad_up = false, $passKey = null)
{
    $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $i = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    if ($passKey !== null) {
        // Although this function's purpose is to just make the
        // ID short - and not so much secure,
        // with this patch by Simon Franz (http://blog.snaky.org/)
        // you can optionally supply a password to make it harder
        // to calculate the corresponding numeric ID
       $len = strlen($index); 


        $passhash = hash('sha256',$passKey);
        $passhash = (strlen($passhash) < $len)
            ? hash('sha512',$passKey)
            : $passhash;

        for ($n=0; $n < $len; $n++) {
            $p[] = substr($passhash, $n ,1);
        }

        array_multisort($p, SORT_DESC, $i);
        $index = implode($i);
    }

    $base = strlen($index);

    if ($to_num) {
        // Digital number <<-- alphabet letter code
        $in = strrev($in);
        $out = 0;
        $len = strlen($in) - 1;
        for ($t = 0; $t <= $len; $t++) {
            $bcpow = bcpow($base, $len - $t);
            $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
        }

        if (is_numeric($pad_up)) {
            $pad_up--;
            if ($pad_up > 0) {
                $out -= pow($base, $pad_up);
            }
        }
    } else {
        // Digital number -->> alphabet letter code
        if (is_numeric($pad_up)) {
            $pad_up--;
            if ($pad_up > 0) {
                $in += pow($base, $pad_up);
            }
        }

        $out = "";
        for ($t = floor(log10($in) / log10($base)); $t >= 0; $t--) {
            $a = floor($in / bcpow($base, $t));
            $out = $out . substr($index, $a, 1);
            $in = $in - ($a * bcpow($base, $t));
        }
        $out = strrev($out); // reverse
    }

    return $out;
}

如您所见,没有什么大的不同,只有我从for循环中删除了strlen,并将其存储在变量中,并为索引预先计算了数组(有点笨拙...但是数组的产生是由数组组成的)大量的计算).

As you can see no major difference, only I removed the strlen from the for loops and stored it in a variable and precalculated the array for the index (a bit clumsy... but the array generation is what was making up the bulk of the computation).

应交的款项:这是原始作者信息:* @作者凯文·范·桑内维尔德(Kevin van Zonneveld)* @作者西蒙·弗朗兹(Simon Franz)* @copyright 2008 Kevin van Zonneveld(kevin dot vanzonneveld dot net)* @license www dot opensource dot org/licenses/bsd-license dot php新的BSD许可证* @version SVN:发布:$ Id:alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $* @link kevin点vanzonneveld点网

Credit where its due: here is the original authors info:* @author Kevin van Zonneveld * @author Simon Franz* @copyright 2008 Kevin van Zonneveld (kevin dot vanzonneveld dot net)* @license www dot opensource dot org/licenses/bsd-license dot php New BSD Licence* @version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $* @link kevin dot vanzonneveld dot net

我无法发布该网址,因为我的信誉很低:S

I can't post the url's as I have to low reputation :S

推荐答案

我进行了一些细微的优化,以消除此处和此处的一些额外CPU周期.大多数情况下,诸如不必要的分配,额外的比较等之类的事情.此外,字符串也可以被视为数组,因此我也在其中进行了处理:

I've made some slight optimizations to remove some extra CPU cycles here and there. Mostly things like unnecessary assignments, extra comparisons, etc. Also, strings can be treated as arrays, so I worked that in, too:

function alphaID($in, $to_num = false, $pad_up = false, $passKey = null)
{
    static $passcache;
    if(empty($passcache))
        $passcache = array();

    $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $i = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    if (!empty($passKey)) {
        // Although this function's purpose is to just make the
        // ID short - and not so much secure,
        // with this patch by Simon Franz (http://blog.snaky.org/)
        // you can optionally supply a password to make it harder
        // to calculate the corresponding numeric ID

        if(isset($passcache[$passKey]))
            $index = $passcache[$passKey];
        else {
            if(strlen($passhash = hash('sha256',$passKey)) < strlen($index))
                $passhash = hash('sha512',$passKey);

            $p = str_split($passhash);

            array_multisort($p, SORT_DESC, $i);
            $index = implode($i);
            $passcache = $index;
        }
    }

    $base = strlen($index);

    if ($to_num) {
        // Digital number <<-- alphabet letter code
        $in = strrev($in);
        $out = 0;
        $len = strlen($in) - 1;
        for ($t = 0; $t <= $len; $t++) {
            $bcpow = bcpow($base, $len - $t);
            $out += strpos($index, $in[$t]) * $bcpow;
        }

        if (is_numeric($pad_up)) {
            $pad_up--;
            if ($pad_up > 0) {
                $out -= pow($base, $pad_up);
            }
        }
    } else {
        // Digital number -->> alphabet letter code
        if (is_numeric($pad_up)) {
            $pad_up--;
            if ($pad_up > 0) {
                $in += pow($base, $pad_up);
            }
        }

        $out = "";
        for ($t = floor(log10($in) / log10($base)); $t >= 0; $t--) {
            $bcp = bcpow($base, $t);
            $a = floor($in / $bcp);
            $out .= $index[$a];
            $in -= $a *  $bcp;
        }
        $out = strrev($out); // reverse
    }

    return $out;
}

我已更新为包含缓存.立即尝试!

I've updated to include a cache. Try it now!

这篇关于php-帮助提高此youtube样式url生成器的效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 04:14