Categories
Code Programming WordPress

__DIR__ vs dirname( __FILE__ )

Calling a constant should always be faster than a function call, right? PHP contains two different ways to get the directory that a file is currently in. __DIR__ is a magic constant added to PHP in 5.3. The official PHP documentation specifically states: “This is equivalent to dirname(__FILE__).” Equivalent in return doesn’t necessarily mean equivalent in performance though, so I decided to test the performance of both accross a couple of variables.

Testing Methodology

When running lots of tests, I’ve found that travis-ci can be a big helper. You can define the matrix of the tests you want to run as your testing matrix and then use the results. So I setup a repo for my tests. The repo contains four main pieces:

  1. A file generator which creates the code I’ll actually run the performance test on.
  2. A test running script which runs the actual tests.
  3. The travis.yml file to define my tests matrix
  4. Files used for the actual test. This is not linked, there are 400,000 files. The generator shows them off.

My test calls the function I am looking for 100,000 each. One run has all the files in 1 directory, and the other has one file per directory. I wanted to rule out the possibility that a single directory could affect this call.

I ran this test on six version of PHP: 5.6.40, 7.0.33, 7.1.27, 7.2.15, 7.3.9 and 7.4-dev. Each test did 650 runs and I looked at the user time for each of those runs. This means that I called both __dir__ and dirname( __file__ ) 130,000,000 times each. I then took the times for each of these runs and looked at that maximum (slowest) run for each and the 95% time ( the average between the 618th and 619th slowest) to rule out any extreme cases. Overall, the results between maximum and 95% are similar.

User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

Stack Overflow

__DIR__ vs dirname( __FILE__) Test Results

95% performance

Script5.67.07.17.27.37.4
dir ( 1 folder)1.42.762.082.512.271.3615
dir ( many folders)1.382.292.552.36152.651.57
dirname( 1 folder)1.58152.272.74153.242.411.5215
dirname( many folders)1.682.292.08153.292.68151.87

Overall, the results show that __dir__ is generally faster, but it’s rarely much faster. Consider that in the slowest run ( using PHP 7.2 and calling dirname( __FILE__ ) on multiple folders), each call took 0.0000329 seconds. The fastest run was 2 hundred-thousandths of a second faster. This is such a micro optimization that except under extreme scale, it’s unlikely you will ever notice a difference. That said, under most versions of PHP, __DIR__ is faster.

So: __dir__ or dirname?

At the end of the day, the performance of __DIR__ is ever so slightly better, but more importantly I think it’s clearer and easier to read. So I’m on team __dir__, but the performance is only a bonus.

If you have a different thought, or think that my testing methodology could be improved, please let me know in the comments below.

Categories
Art Design Four Short Things Programming

Four Short Things – 9 February 2019

Inspired by O’reilly’s Four Short Links, here are some of the things I’ve seen, read, or watched recently.

The Value of Good Design

MoMA’s spring exhibition includes a show featuring everyday objects, the types that it’s feasible to find in our homes. Brooms, Rakes, Chairs, A Slinky. With an emphasis on work that appeared in shows from the 1930’s to 1950’s, there is plenty of Eames, Saarinen, and Bruan to make any home goods nerd geek out. In addition to the main section of the show, there is a small lab where you can couch and sit on some of the items on display. It’s open until June 15.

Terraform

Describing itself as “Write, Plan, and Create Infrastructure as Code”, terraform allows for almost every part of your infrastructure to happen as code. You can thus keep your DNS in GitHub. You can keep your GitHub config in GitHub too.

What’s new in PHP7.4?

Odds are, you aren’t running PHP7.3 yet, but that doesn’t mean work hasn’t started on PHP7.3. Heck, 8.0 is already being planned. It’s still early, but coalesce assignment is my prediction for what is going to cause the most useless arguments and also be the biggest win.

Inclusive Design: Who’s Opportunity is it?

My friend David uses his journey to help explain how inclusive design is a win for everyone. He looks at Inclusive design as an opportunity for business, content, quality, performance, and people. Definitely was one of the best things I read this week.

Four Short Things is a series where I post a small collection of links to art, news, articles, videos and other things that are me. Follow my RSS feed to see Four Short Things whenver it comes out.

Categories
About Aaron Uncategorized WordPress

Fall Conferences – PHP Madison and WordCamp NYC

Today I get to announce two conferences that I’m speaking at this fall. The first is here in NYC and is the 2015 WordCamp NYC.  I’ll be giving a talk entitled:
Lessons from Science Fiction and Fantasy we can use in Creating Websites.  Here is a short synopsis.

Science Fiction and Fantasy can teach web creators many valuable lessons. From seeing how Daleks with too narrow of a goal always fail to understanding the Klingons value of honor, to hundreds of other we can become better web creators by borrowing lessons from Science Fiction and Fantasy.

Next, I’ll be traveling to Madison, Wisconsin for the first time in almost 10 years to present “How Not To Build A WordPress Plugin” at Madison PHP.  A short synopsis of this talk is

WordPress has a powerful plugin architecture that enables you to build almost anything on top of WordPress. This power though can lead to anti-patterns that slow down sites, confuse users, and make it hard to scale. Let’s look at the wrong way of building plugins so you can avoid these traps.

Tickets for both events are on sale.  If you are either one, make sure to say hi!

Categories
Uncategorized WordPress

Following up on WordPress in a Next Generation PHP World

In April, just as WordPress 4.3 was beginning development, I started a conversation about WordPress, PHP7, and HHVM. Now that WordPress 4.3 has been released, I’m glad to say WordPress is looking great as far as PHP7 goes.

I’m planning on spending some time during the 4.3 development cycle focused on these next generation platforms.

The PHP core team did a solid job of not introducing many breaking changes with this release, which really helped to make the transition easier. The two major changes that WordPress needed to make in order to have passing unit tests on PHP7 were to deprecate PHP4 style constructors and updating some variable variables.

The final commit of WordPress 4.3 has PHP7 tests passing and running faster then any of the other PHP versions.

In 4.4, I intend to continue to focus on PHP7.  The release schedule targets Mid October 2015. I hope to move PHP7 out of the Allowed Failures bucket on Travis-CI the day it is released.

Next up is getting the unit tests passing on HHVM. Onward!

Categories
Code Uncategorized WordPress

WordPress in a next generation PHP world

HHVM has now released it’s second long term support release and PHP 7 is in the final stages of implementing changes. It’s an exciting time for PHP and to be a PHP developer which means it is also an exciting time to be a WordPress developer since it creates an opportunity for WordPress to once again embrace forwards compatibility.

While I was at PHPUK, one of the most common conversations I had was people being critical of WordPress for supporting PHP 5.2 as a minimum.  Many of those same people became less critical once they find out WordPress runs great on PHP 5.6 and that many people run it on HHVM.

For the last several weeks, WordPress has been running it’s unit tests on PHP7 nightly builds. They’ve been running on HHVM for months. Right now, the unit tests are not passing for either one and as far as I know, have never passed for either one.  This is a problem.

I’m planning on spending some time during the 4.3 development cycle focused on these next generation platforms. Rasmus has put together a php7 vagrant box and JJJ created an addon to Varying Vagrant Vagrants to enable HHVM there. WP engine also has it’s own WordPress HHVM vagrant box. I intend to use all three of these to help.

Davey Shafik has put together a great two part series on the changes coming in PHP 7.  The two changes that are most likely to cause issues for WordPress sites are the removal of all deprecated features and the deprecation of PHP4 style constructors. This is going to affect many widgets along with all sorts of other code.

It’s exciting to see PHP moving forward.  The competition between HHVM and PHP runtimes is making PHP faster and is only going to push the language forward. It’s a great time to be writing PHP.

Categories
Code Programming Uncategorized WordPress

Adding a twitter box to the Profile page in WordPress

I haven’t used AIM in years and I haven’t used Yahoo Messager in even longer.  What I do use is twitter.  Therefore, having a box in the WordPress admin for twitter name is better and with some code I wrote for the , super easy.

[php]
add_filter(‘user_contactmethods’, ‘jorbin_user_contactmethods’);

/**
* Removes AIM, YAHOO, and Jabber boxes from profile page and adds Twitter in it’s place
*
*/
function jorbin_user_contactmethods($user_contactmethods){
$new_user_contactmethods[‘twitter’] = __(‘Twitter’);

return $new_user_contactmethods;
}
[/php]

Filtering user_contactmethods is one of the easiest things you can do. All you need to do is return an array where the key is the code based name you want and the value is the public facing name. Easy. As. Pie.

Categories
Code Programming Uncategorized WordPress

Add Role

This simple plugin, built for my WordPress DC Presentation on the Users API adds a role called link master, that gets the capability to edit links and also read private posts and pages and also removes the ability for editors to manage categories.

Categories
Code Programming Uncategorized WordPress

Personalized Read More

Want to make the read more link in WordPress call out the user if they are logged in?  As part of my DC WordPress presentation, i showed that it’s easy as pie and only about 10 lines of code.

Categories
Code Programming Uncategorized WordPress

WordPress Shortcodes for Users and Non Users

One of the plugins I wrote for the DC WordPress group’s meeting on Users / Roles / Capabilities demonstrates how to create shortcode that makes some content only visable by users with a certain capability and other content only visable to non logged in viewers.  You can download Shortcodes for Discrimination (along with the other three ) from google code.  Beyond the jump, I go over the code and an explanation of how it works.

Categories
Code Uncategorized WordPress

How to remove a nav menu location in WordPress 3.0.0

This function is in WordPress 3.0.1. You can now just call unregister_nav_menu and don’t need to put it in your child theme

One of the great additions to WordPress 3.0 is navigation menus.  One of the aspects that it contains is the ability for a theme to register a nav menu location and for the user to assign any menu they want to that location.  GREAT!  Except that a missing piece of functionality is the ability for Child Themes to remove a location that it might no longer want.  I’ve created a ticket and submitted a patch, but if you need that functionality now, just add the code below to you child theme’s functions.php file:
[php]
if (! function_exists(‘unregister_nav_menu’) ):
function unregister_nav_menu( $location ) {
global $_wp_registered_nav_menus;

if (is_array($_wp_registered_nav_menus) && array_key_exists( $location, $_wp_registered_nav_menus ) ){
unset( $_wp_registered_nav_menus[$location] );
return true;
}

return false;

}
endif;
[/php]

Then all you need to do is call:
[php]
//Remove Twenty Ten Default Menu Location
unregister_nav_menu(‘primary’);[/php]
At some point after the nav menu is registered. I like to use the init hook.