Which is faster? Constants, Variables or Variable Arrays
Constants defined using define()
are fairly slow in PHP. People actually wrote extensions (like hidef) to improve the performance.
But unless you have loads of constants this shouldn't make much of a difference.
As of PHP 5.3 you can also use compile-time constants using const NAME = VALUE;
. Those are much faster.
Should I prefer variables or multiple indirections on arrays in C# in perf critical code?
In general access to an array is slower than a temporary because it has 2 additional pieces of overhead
- A layer of indirection
- A bounds check
However I would not be changing any code I had today based on that knowledge unless a profiler clearly showed a tight loop to be a significant perf problem in my application. And furthermore showed that changing to a cached local produced a significant perf benefit.
Why are variable assignments faster than calling from arrays in python?
Python operations and memory allocations are generally much slower than Numpy's highly optimized, vectorized array operations. Since you are looping over the array and allocating memory, you don't get any of the benefits that Numpy offers. It's especially bad in your first one because it causes an undue number of allocations of small arrays.
Compare your code to one that offloads all the operations to Numpy instead of having Python do the operations one by one:
def test3(coords):
mins = (coords - 1)**2
results = np.sqrt(np.sum(mins, axis=1))
return results
On my system, this results in:
Test 1 speed: 4.995761550962925
Test 2 speed: 1.3881473205983639
Test 3 speed: 0.05562112480401993
Java: Is there a performance difference between using n variables or using hard coded array elements?
You are most likely faster with separate variables.
Why? The JVM optimizes your code at runtime to make it faster. It tracks the flow and values of each variable to learn how to optimize code. For example it might realize that a parameter to a method is never assigned and thus constant. But it does not do so for each element of an array. An array is just considered as one huge mutable variable. So you are more likely to get optimal code when using separate variables.
Constant pointer array or pointer to an array ? What is faster in C?
They're all compiled in the same way, *(pointer_to_first_element + x + y)
.
Related Topics
Fatal Error: Call to a Member Function Bindparam()
Modify an Existing PHP Function to Return a String
How to Do Url Rewriting in PHP
Looping a SQL Insert Statement with Dates
PHP Prepared Statement Problems
Disable Template Caching for Development in Opencart 3
PHP Sending Variables to File_Get_Contents()
Differencebetween the | and || Operators
Getting Warning "Header May Not Contain More Than a Single Header, New Line Detected"
PHP Removing HTML Tags from String
What Does It Mean to Run PHP in Quiet Mode