Change language

PHP for vs foreach performance

Syntax:
for (initialization; condition; increment / decrement) {// Set of Code to be iterated and executed} 
Each of for loop parameters has unique functions, which are described below for better understanding:
  • initialization:is used to initialize iterator variables and is executed concurrently without the condition statement being executed during the loop condition start , which is the very first execution of a set of code in a loop.
  • condition:at the beginning of each iteration, the condition statement is executed, and if the condition returns true, the loop continues and the nested statements in the set are executed code. If the condition evaluates to false, execution of the loop is interrupted at that point in the code.
  • Increment:This increments the loop counter with a new increment value that should be evaluated for the conditional statement. It must be executed at the end of each iteration without interruption.
Example:This example uses a for loop that starts at $j = 1. The loop will continue until until $j is less than or equal to 5. The $j variable will increment by 1 each time the loop starts.  
// This is where the loop starts for ( $j = 1; $j < = 5; $j ++) { echo $j . "GeeksforGeeks" ; }  
?>
Exit:
1 GeeksforGeeks 2 GeeksforGeeks 3 GeeksforGeeks 4 GeeksforGeeks 5 GeeksforGeeks 
foreach:The purpose of the foreach loop is to determine the usage, and it deliberately iterates over the elements of the array data structure.Syntax:
foreach ($array as $element) {// PHP Code to be executed} 
foreach ($array as $key = > $element) {// PHP Code to be executed} 
Below are examples to illustrate the use of the foreach loop.Example 1:

$students = array ( "Jimmy" , "Jonny" , "Jacky" );  
// Loop through an array of students foreach ( $students as $element ) { echo $element . "
"
; }  
?>
Exit:
Jimmy 
Jonny
Jacky
Example 2: $employee = array ( "name" = > " Robert " , " email " = > " robert112233 @ mail. com " , " age " = > 18, " gender " = > "male" 
);  
// Loop through an array of employees foreach ( $employee as $key = > $element ) { echo $key . ":" . $element . "
"
; }  
?>
Exit:
name: Robert 
email: [email protected]
age: 18
gender: male
Performance comparison in for and foreach loops:
  • The for loop is considered open to iterate, while the foreach hides the iteration and is noticeably simplified.
  • The foreach loop is considered to be much better in performance than the generic for loop.
  • The foreach loop, while iterating over an array of elements, is easier to execute and relatively completes the loop in less time.
  • The foreach loop allocates temporary memory for index iterations, making the entire system overhead in terms of memory allocation.
Example :
// PHP Performance Comparison Program // for and foreach loop 
// Declare the $element array $elements = array ();  
// array of 100 units with random string values ​​ for ( $i = 0; $i < = 100; $i ++) { $units [] = (string) rand (11111, 99999); } $startingTime = microtime (true);  
// to evaluate loop performance for ( $i = 0; $i < count ( $units ); $i ++) {} $endingTime = microtime (true); $forlooptime = $endingTime - $startingTime ; $startingTime = microtime (true);  
// evaluate the performance of the foreach loop foreach ( $units as $unit ) {} $endingTime = microtime (true); $foreachlooptime = $endingTime - $startingTime ; echo " for loop evaluates to: ". number_format ( $forlooptime * 1000, 3); echo " foreach loop evaluates to: " . number_format ( $foreachlooptime * 1000, 3);  
?>
Exit:
for loop evaluates to: 0.008 foreach loop evaluates to: 0.003 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method