Register Register Member Login Member Login Member Login Forgot Password ??
PHP Tutorial (Hypertext Preprocessor), ASP Tutorial (Active Server Pages), ASP.NET Tutorial (.Net 1.1,2.0,3.5)
   
   
  Registered : 70,501
   
 
HOME > Community Zone > เปรียบเทียบความเร็วการทำงานระหว่าง for กับ foreach By DS_Ohm
Search

เปรียบเทียบความเร็วการทำงานระหว่าง for กับ foreach By DS_Ohm

 
 
class_timer.php
<?php
/**
 * DS Timer v2.0.0
 * 
 * @name Timer Class
 * @version 2.0.0
 * @author Narong Rammanee, <ranarong@live.com>
 * @copyright Copyright (c) 2010, Narong Rammanee
 * @license No Licence Free 2010
 */
class Timer {
	private $elapsedTime;
	private $unitTime;

	/**
	 * Start Timer
	 * 
	 * @return void
	 */ 
	public function start() {
		
		if(!$this->elapsedTime = $this->getMicrotime())
			throw new Exception('Error obtaining start time!');
	}

	/**
	 * Stop Timer
	 * 
	 * @return stop time
	 */ 
	public function stop() {
		
		if(!$this->elapsedTime = (round($this->getMicrotime() - $this->elapsedTime, 6) * 1000000))
				throw new Exception('Error obtaining stop time!');
				
		$this->unitTime = self::setUnitTime();
		
		return $this->elapsedTime.$this->unitTime;
	}

	/**
	 * define private 'getMicrotime()' method
	 * 
	 * @return micro second time
	 */ 
	private function getMicrotime() {
		
		list($useg, $seg) = explode(' ', microtime());
		return ((float)$useg + (float)$seg);
	}
	
	/**
	 * define private 'setUnitTime()' method
	 * 
	 * @return time unit 
	 */
	private function setUnitTime() {
		
		if($this->elapsedTime < 1000) {
			$this->unitTime	= '&micro;s';		
		}
		else if($this->elapsedTime >= 1000 && $this->elapsedTime < 1000000) {
			$this->elapsedTime /=  1000;
			$this->unitTime = 'ms';
		}
		else if($this->elapsedTime >= 1000000) {
			$this->elapsedTime /=  1000000;
			$this->unitTime = 's';
		}
		return $this->unitTime;
	}
?>


test_timer.php
<style type="text/css">
	body { font:13px Tahoma, Geneva, sans-serif; }
	.txt-red { color:red; }
</style>
<?php
	require_once('class/class_timer.php');
	
	# assign array $val
	$val = range(0, 100);
	
	# assign array $value
	$value = range(0, 100);
	
	# create timer instance
	$t = new Timer;
	
	# for loop start time
	$t->start();
	
	for( $i = 0 ; $i < count($val) ; $i++ )
		for ($j = 0; $j < count($value); $j++) { }
	# for loop used time
	$tt = $t->stop();
	
	# show for loop used time
	echo 'for loop used time : <span class="txt-red">'.$tt.'</span><br />';
	
	# foreach start time 
	$t->start();
	foreach ( $val as $name ) 
		foreach ( $value as $name ) { }
	# foreach used time
	$tt = $t->stop();
	
	# show foreach loop used time
	echo 'foreach loop used time : <span class="txt-red">'.$tt.'</span><br />';
?>





เครดิต : Timer class
Demo :- Downlaod



 

  By : DS_Ohm
  Score Rate :
  Create Date : 2010-01-25 17:14:38
  View : 351
  Download : No files
     


Sponsored Links
 
 
Ads Contact : 08-1987-6107
Download Safari
 
Ads Contact : 08-1987-6107

Related Links
ส่งอีเมล์ง่าย ๆ ด้วย ArgoSoft Mail Server
ส่งอีเมล์ง่าย ๆ ด้วย ArgoSoft Mail Server
ArgoSoft Mail Server เป็น Mail Server ที่สำหรับส่งอีเมล์ออกไปข้างนอก โดยสามารถใช้ได้กับทุก Application ที่เรียกส่งผ่าน SMTP
Rate :
Update :
2009-01-11 09:13:49 View : 2,999
Import MySQL to Access Database เทคนิคการอิมพอร์ทฐานข้อมูล MySQL เข้าสู่ฐานข้อมูล Access
Import MySQL to Access Database เทคนิคการอิมพอร์ทฐานข้อมูล MySQL เข้าสู่ฐานข้อมูล Access
เทคนิคการนำเข้าฐานข้อมูล MySQL ไปยังฐานข้อมูล Access ผ่าน Driver ODBC แบบง่าย ๆ สะดวกรวดเร็วและใช้งานง่ายมาก ๆ ครับ
Rate :
Update :
2009-02-08 23:19:04 View : 2,530
PHP Class create table and search By DS_Ohm
PHP Class create table and search By DS_Ohm
PHP Class create table and search By DS_Ohm
Rate :
Update :
2010-01-27 17:08:09 View : 566
เขียน JavaScript ใช้งาน Cookie แบบง่าย ๆ
เขียน JavaScript ใช้งาน Cookie แบบง่าย ๆ
รู้วิธีการใช้งาน JavaScript เพื่อเก็บค่าต่าง ๆ ของ Client ลงใน Cookie ของ Web Browser เช่นค่าที่ผู้ใช้กรอกตรงใน Input หรือ textarea
Rate :
Update :
2009-01-14 12:18:52 View : 2,086
โค๊ด PHP ตรวจสอบข้อมูล บน LDAP
โค๊ด PHP ตรวจสอบข้อมูล บน LDAP
ตัวอย่าง โค๊ด PHP สำหรับตรวจสอบ USERNAME PASSWORD ผ่าน LDAP
Rate :
Update :
2009-01-11 15:18:58 View : 2,067
PHP class simple calculator By DS_Ohm
PHP class simple calculator By DS_Ohm
PHP class simple calculator By DS_Ohm
Rate :
Update :
2010-01-27 17:12:01 View : 343
 
 
© www.ThaiCreate.Com. 2003-2009 All Rights Reserved. My network : ThaiGoodWeb.Com , DVDmono.com
Comments & Suggestions Please Contact Us : thaicreate at hotmail dot com [Conditions Privacy Statement] ติดต่อโฆษณา 01-987-6107 อัตราราคา คลิกที่นี่