PHP with cache and no caching performance test

Source: Internet
Author: User
Tags redis

Test program execution time in various environments in thinkphp projects
do not use caching:

The code is as follows Copy Code

<?php
Header ("Content-type:text/html;charset=utf-8");

$starttime =caltime ()//Start time

$articles =array ();

Loop out 500 article information
For ($i =0 $i <100; $i + +) {
$sql = "Select a.*,c.* from Blog_article as a,blog_category as C where a.reid=c.id limit 0,5";
$articles =array_merge ($articles, M (' article ')->query ($sql));
}

$overtime =caltime ();//End time

Echo ' does not use cached conditions the program execution time is: '. ($overtime-$starttime). ' Seconds ';

?>

Result: The program execution time is 0.0600001811981 seconds without the use of a cache condition:

File Caching:

  code is as follows copy code

<?php
Header (" Content-type:text/html;charset=utf-8 ");

$starttime =caltime ();//Start time

$articles =s (' articles ');
if (! $articles) {
   $articles =array ()
  //Loop out 500 www.111cn.net article Information
    for ($i =0 $i <100; $i + +) {
       $sql = ' Select a.*,c.* from Blog_article as a, Blog_category as C where a.reid=c.id limit 0,5 ";
       $articles =array_merge ($articles, M (' article ')->query ($sql));
  }
   S (' articles ', $articles, 60);
}

$overtime =caltime ();//End time

Echo uses file caching conditions for program execution time: '. ( $overtime-$starttime). ' Seconds ';
?

Results: The program execution time using file caching is 0.00999999046326 seconds

The code is as follows Copy Code

<?php
Header ("Content-type:text/html;charset=utf-8");
      
$starttime =caltime ()//Start time

$mem =new Memcache ();
if (! $mem- >connect (' 127.0.0.1 ', 11211)} {
   echo ' connection failed ';
}

$articles = $mem->get (' articles ');

if (! $articles) {
   $articles =array ();
  //Loop out 500 article Information
   for ($i =0; $i <100; $i + +) {
       $sql = ' Select a.*,c.* from Blog_article as a,blog_category as C Where a.reid=c.id limit 0,5 ";
       $articles =array_merge ($articles, M (' article ')->query ($sql));
  }
   $mem->set (' articles ', $articles, memcache_compressed,60);
}

$overtime =caltime ();//End time

Echo ' uses memcache cache conditions for program execution time: '. ( $overtime-$starttime). ' Seconds ';
?

Results: The program execution time is 0.00999999046326 seconds using the memcache cache condition

The code is as follows Copy Code

<?php
Header ("Content-type:text/html;charset=utf-8");
      
$starttime =caltime ()//Start time

$redis =new Redis ();
$redis- >connect (' 127.0.0.1 ', ' 6379 ');

if (! $redis) {
   echo ' connection failed ';
}

$articles = $redis->get (' articles ');

if (! $articles) {
   $articles =array ()
  //Loop out 500 www.111cn.net article Information
    for ($i =0 $i <100; $i + +) {
       $sql = ' Select a.*,c.* from Blog_article as a, Blog_category as C where a.reid=c.id limit 0,5 ";
       $articles =array_merge ($articles, M (' article ')->query ($sql));
  }
   $redis->setex (' articles ', $articles);
}

$overtime =caltime ();//End time

Echo ' uses Redis cache conditions for program execution time: '. ( $overtime-$starttime). ' Seconds ';

?

Results: The program execution time is 0.00999999046326 seconds using the Redis cache condition

Visible with caching, the program executes 6 times times faster than it does when the cache is not used,

But different caches, because the data is not very large, almost no difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.