php - Bu youtube tarzı url jeneratör verimliliğini artırmaya yardımcı

3 Cevap php

Bazı arama yaptıktan sonra ancak ben bunu bir çok kullanılan olacak gibi verimliliğini artırmak için umut ediyorum ... özgün kimliği gizlemek için şifreleme ile bu youtube tarzı url jeneratör bulundu. Şimdiye kadar% 20 oranında düzeldi ... Herkes bana daha iyileştirmeye yardımcı olabilir.

Bu orijinal:

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;
}

İşte benim değiştirilmiş kod şimdiye kadar:

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;
}

Eğer önemli bir fark göremiyorum gibi, sadece ben döngüler için gelen strlen çıkarılır ve bir değişkene depolanan ve endeks için dizi (biraz beceriksiz önceden hesaplanmış ... ama dizi nesil ne toplu oluşturan idi hesaplama).

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

Ben düşük itibar gibi ben URL açamazsınız: S

3 Cevap

Ben orada burada bazı ekstra CPU döngülerini kaldırmak için bazı ufak optimizasyonlar yaptık. Çoğunlukla vb gereksiz atamaları, ekstra karşılaştırmalar, gibi şeyler Ayrıca, dizeleri diziler olarak tedavi edilebilir, bu yüzden de, bu çalıştı:

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;
}

Düzenleme: Ben bir önbellek içerecek şekilde güncelledik. Şimdi deneyin!

Eğer gereksiz aramaların kurtulmak iyi bir iş yapmış gibi görünüyor.

Birkaç birine azaltılmış olabilir ($ tabanı, $ t) bcpow çağrıları var gibi görünüyor ...

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

Muhtemelen bir fark olmaz.

Ben sadece geniş .. bana neden açıklayayım bir dizin oluşturmak ve bu siteyi kullanmanız gerektiğini düşünüyorum ...

Karma ve herşey yukarıda '$ base = strlen ($ dizin);' Sadece dizin oluşturmak ... Eğer bir url çözmek için aynı $ parola kullanmak zorunda olacak. Eğer passkey kullanıldığı bilmenin bir yolu yok gibi geniş o site yapmak zorunda kalacak. Url oluşturmak için her arama aynı parolayı kullanmak ve böylece aynı dizin üretecek anlamına gelecektir.

Ve böylece ... Ben endeks oluşturma kodu şerit ve benzersiz bir dizin oluşturur ve '$ index' değişkende saklamak istiyorum.