SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Hell
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy could anyone make this function run more faster?

    Code:
    function VN2none($str)
    {
    	//khoi tao string
    	$vstr = 'aAeEoOuUiIdDyY';
    	$Thga = 'ạảấầậẩẫăắằặẳẵ';
    	$HoaA = 'ẠẢẤẦẬẨẪĂẮẰẶẲẴ';
    	$Thge = 'ẹẻẽếềệểễeeeeee';
    	$HoaE = 'ẸẺẼẾỀỆỂỄEEEEEE';
    	$Thgo = 'ọỏốồộổỗơớờợởỡ';
    	$HoaO = 'ỌỎỐỒỘỔỖƠỚỜỢỞỠ';
    	$Thgu = 'ụủũưứừựửữuuuuuu';
    	$HoaU = 'ỤỦŨƯỨỪỰỬỮUUUUUU';
    	$Thgi = 'ịỉĩiiiiiiiiiiii';
    	$HoaI = 'ỊỈĨIIIIIIIIIIII';
    	$Thgd = 'đdddddddddddddddd';
    	$HoaD = 'ĐDDDDDDDDDDDDDDDD';
    	$Thgy = 'ỳỵỷỹyyyyyyyyyyyy';
    	$HoaY = 'ỲỴỶỸYYYYYYYYYYYY';
    	
    	// khoi tao mang
    	for ($i = 0; $i <= 13; $i++)
    	{
    		$arr_matrix[$i][0] = utf8_substr($vstr, $i, 1);
    	}
    	
    	// nap ma tran
    	for ($j = 1; $j <= 17; $j++)
    	{
    		for ($i = 1; $i <= 17; $i++)
    		{
    			$arr_matrix[0][$i] = utf8_substr($Thga, $i-1, 1);
    			$arr_matrix[1][$i] = utf8_substr($HoaA, $i-1, 1);
    			$arr_matrix[2][$i] = utf8_substr($Thge, $i-1, 1);
    			$arr_matrix[3][$i] = utf8_substr($HoaE, $i-1, 1);
    			$arr_matrix[4][$i] = utf8_substr($Thgo, $i-1, 1);
    			$arr_matrix[5][$i] = utf8_substr($HoaO, $i-1, 1);
    			$arr_matrix[6][$i] = utf8_substr($Thgu, $i-1, 1);
    			$arr_matrix[7][$i] = utf8_substr($HoaU, $i-1, 1);
    			$arr_matrix[8][$i] = utf8_substr($Thgi, $i-1, 1);
    			$arr_matrix[9][$i] = utf8_substr($HoaI, $i-1, 1);
    			$arr_matrix[10][$i] = utf8_substr($Thgd, $i-1, 1);
    			$arr_matrix[11][$i] = utf8_substr($HoaD, $i-1, 1);
    			$arr_matrix[12][$i] = utf8_substr($Thgy, $i-1, 1);
    			$arr_matrix[13][$i] = utf8_substr($HoaY, $i-1, 1);
    		}
    	}
    	
    	// loai bo dau
    	$str = trim($str);
    	for ($j = 0; $j <= 13; $j++)
    	{
    		for ($i = 1; $i <= 17; $i++)
    		{
    			$str = str_replace($arr_matrix[$j][$i], $arr_matrix[$j][0], $str);
    		}
    	}
    	$str = url_title($str, '_');
    	return $str;
    }
    
    function utf8_substr($str, $from, $len)
    {
    	return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
                           '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
                           '$1',$str);
    }

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,226
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    make faster

    The only thing I see that might make it (IMHO insignificantly) faster would be to use a variable to hold the $i-1 value instead of calculating it each line. ie.
    PHP Code:
            for ($i 1$i <= 17$i++)
            {
                
    $value $i-1;
                
    $arr_matrix[0][$i] = utf8_substr($Thga$value1);
                
    $arr_matrix[1][$i] = utf8_substr($HoaA$value1);
                
    $arr_matrix[2][$i] = utf8_substr($Thge$value1);
    ...... 
    I imagine the regex is the main consumer, not much you can do about that.

  3. #3
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looking at your code, $arr_matrix is going to be identical each and every time this function is run. So probably the single best thing you could do to improve the speed is to not regenerate it each time but to simply hard-code it into your function. If you don't like that, try adding some basic memoization to your function, although this will only improve your speed if it is called multiple times per script.

    I really think just hard-coding $arr_matrix instead of regenerating the whole thing each time is by far your best bet.
    PHP questions? RTFM
    MySQL questions? RTFM

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's one way.
    PHP Code:
    function VN2none($str)
    {
        
    //khoi tao string
        
    static $vstr 'aAeEoOuUiIdDyY';
        static 
    $Thga '???????a?????';
        static 
    $HoaA '???????A?????';
        static 
    $Thge '????????eeeeee';
        static 
    $HoaE '????????EEEEEE';
        static 
    $Thgo '???????o?????';
        static 
    $HoaO '???????O?????';
        static 
    $Thgu '??uu?????uuuuuu';
        static 
    $HoaU '??UU?????UUUUUU';
        static 
    $Thgi '??iiiiiiiiiiiii';
        static 
    $HoaI '??IIIIIIIIIIIII';
        static 
    $Thgd 'ddddddddddddddddd';
        static 
    $HoaD 'DDDDDDDDDDDDDDDD';
        static 
    $Thgy '????yyyyyyyyyyyy';
        static 
    $HoaY '????YYYYYYYYYYYY';
        static 
    $arr_matrix = array();
        
        if(!
    $arr_matrix)
        {
            
    // khoi tao mang
            
    for ($i 0$i <= 13$i++)
            {
                
    $arr_matrix[$i][0] = utf8_substr($vstr$i1);
            }
            
            
    // nap ma tran
            
    for ($j 1$j <= 17$j++)
            {
                for (
    $i 1$i <= 17$i++)
                {
                    
    $arr_matrix[0][$i] = utf8_substr($Thga$i-11);
                    
    $arr_matrix[1][$i] = utf8_substr($HoaA$i-11);
                    
    $arr_matrix[2][$i] = utf8_substr($Thge$i-11);
                    
    $arr_matrix[3][$i] = utf8_substr($HoaE$i-11);
                    
    $arr_matrix[4][$i] = utf8_substr($Thgo$i-11);
                    
    $arr_matrix[5][$i] = utf8_substr($HoaO$i-11);
                    
    $arr_matrix[6][$i] = utf8_substr($Thgu$i-11);
                    
    $arr_matrix[7][$i] = utf8_substr($HoaU$i-11);
                    
    $arr_matrix[8][$i] = utf8_substr($Thgi$i-11);
                    
    $arr_matrix[9][$i] = utf8_substr($HoaI$i-11);
                    
    $arr_matrix[10][$i] = utf8_substr($Thgd$i-11);
                    
    $arr_matrix[11][$i] = utf8_substr($HoaD$i-11);
                    
    $arr_matrix[12][$i] = utf8_substr($Thgy$i-11);
                    
    $arr_matrix[13][$i] = utf8_substr($HoaY$i-11);
                }
            }
        }
        
        
    // loai bo dau
        
    $str trim($str);
        for (
    $j 0$j <= 13$j++)
        {
            for (
    $i 1$i <= 17$i++)
            {
                
    $str str_replace($arr_matrix[$j][$i], $arr_matrix[$j][0], $str);
            }
        }
        
    $str url_title($str'_');
        return 
    $str;

    Quote Originally Posted by kromey View Post
    Looking at your code, $arr_matrix is going to be identical each and every time this function is run. So probably the single best thing you could do to improve the speed is to not regenerate it each time but to simply hard-code it into your function.
    I agree but if you go down that road make it a static variable too otherwise it still has to be built each time the function rolls which in a big loop really uses a lot of effort.

  5. #5
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Hell
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    thank you all for replying me, after ask on some forum I got this solution
    PHP Code:
    function VN2none ($str) {
    static 
    $from =
              
    'ạảấầậẩẫăắằặẳẵ'.
              
    'ẠẢẤẦẬẨẪĂẮẰẶẲẴ'.
              
    'ẹẻẽếềệểễeeeeee'.
              
    'ẸẺẼẾỀỆỂỄEEEEEE'.
              
    'ọỏốồộổỗơớờợởỡ'.
              
    'ỌỎỐỒỘỔỖƠỚỜỢỞỠ'.
              
    'ụủũưứừựửữuuuuuu'.
              
    'ỤỦŨƯỨỪỰỬỮUUUUUU'.
              
    'ịỉĩiiiiiiiiiiii'.
              
    'ỊỈĨIIIIIIIIIIII'.
              
    'đdddddddddddddddd'.
              
    'ĐDDDDDDDDDDDDDDDD'.
              
    'ỳỵỷỹyyyyyyyyyyyy'.
              
    'ỲỴỶỸYYYYYYYYYYYY',
           
    $to =
              
    'aaaaaaaaaaaaaaaaa'.
              
    'AAAAAAAAAAAAAAAAA'.
              
    'eeeeeeeeeeeeeeeee'.
              
    'EEEEEEEEEEEEEEEEE'.
              
    'ooooooooooooooooo'.
              
    'OOOOOOOOOOOOOOOOO'.
              
    'uuuuuuuuuuuuuuuuu'.
              
    'UUUUUUUUUUUUUUUUU'.
              
    'iiiiiiiiiiiiiiiii'.
              
    'IIIIIIIIIIIIIIIII'.
              
    'ddddddddddddddddd'.
              
    'DDDDDDDDDDDDDDDDD'.
              
    'yyyyyyyyyyyyyyyyy'.
              
    'YYYYYYYYYYYYYYYYY';

        return 
    url_title(strtr(trim($str), $from$to), '_');

    note that: this will convert all vietnamese character to an english character in a string. Example: cho cc bạn --> chao cac ban.

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by itmanvn View Post
    thank you all for replying me, after ask on some forum I got this solution
    PHP Code:
    function VN2none ($str) {
    static 
    $from =
              
    'ạảấầậẩẫăắằặẳẵ'.
              
    'ẠẢẤẦẬẨẪĂẮẰẶẲẴ'.
              
    'ẹẻẽếềệểễeeeeee'.
              
    'ẸẺẼẾỀỆỂỄEEEEEE'.
              
    'ọỏốồộổỗơớờợởỡ'.
              
    'ỌỎỐỒỘỔỖƠỚỜỢỞỠ'.
              
    'ụủũưứừựửữuuuuuu'.
              
    'ỤỦŨƯỨỪỰỬỮUUUUUU'.
              
    'ịỉĩiiiiiiiiiiii'.
              
    'ỊỈĨIIIIIIIIIIII'.
              
    'đdddddddddddddddd'.
              
    'ĐDDDDDDDDDDDDDDDD'.
              
    'ỳỵỷỹyyyyyyyyyyyy'.
              
    'ỲỴỶỸYYYYYYYYYYYY',
           
    $to =
              
    'aaaaaaaaaaaaaaaaa'.
              
    'AAAAAAAAAAAAAAAAA'.
              
    'eeeeeeeeeeeeeeeee'.
              
    'EEEEEEEEEEEEEEEEE'.
              
    'ooooooooooooooooo'.
              
    'OOOOOOOOOOOOOOOOO'.
              
    'uuuuuuuuuuuuuuuuu'.
              
    'UUUUUUUUUUUUUUUUU'.
              
    'iiiiiiiiiiiiiiiii'.
              
    'IIIIIIIIIIIIIIIII'.
              
    'ddddddddddddddddd'.
              
    'DDDDDDDDDDDDDDDDD'.
              
    'yyyyyyyyyyyyyyyyy'.
              
    'YYYYYYYYYYYYYYYYY';

        return 
    url_title(strtr(trim($str), $from$to), '_');

    note that: this will convert all vietnamese character to an english character in a string. Example: cho cc bạn --> chao cac ban.
    Make your $to variable static also.

  7. #7
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just out of curiosity, how much faster does this algorithm run now? I've no doubt it's significant, I'm just interested in knowing by how much.
    PHP questions? RTFM
    MySQL questions? RTFM


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •