Jump to content

Get string based on another string


NotionCommotion

Recommended Posts

Given a text value $path, I want to return the actual path.  One option is a switch statement as shown below.  Another option is put them in an associated array (or maybe a static array?) and check if the element is set and if not throw an deception.  Or maybe a totally different solution.  Is one solution better (i.e. more efficient) than another?  I will probably be retrieving a path around 5 times for every server hit. Thanks

    function getPath($path)
    {
      switch($path){
        case 'classes': $path=__DIR__;break;
        case 'application': $path=dirname(__DIR__);break;
        case 'root': $path=dirname(dirname(__DIR__));break;
        case 'twig': $path=dirname(dirname(__DIR__)).'/vendor/autoload.php';break;
        case 'lib': $path=dirname(__DIR__).'/lib';break;
        case 'help': $path=dirname(__DIR__).'/lib/help';
        case 'templates': $path=dirname(__DIR__).'/lib/templates';
        case 'htdocs_base': $path=dirname(dirname(__DIR__)).'/html';
        case 'resources_base': $path=dirname(dirname(__DIR__)).'/user_resources';
        // About 10 more
        default: throw new Exception("Path {$path} not valid.");
      }
      return $path;
    }
 
Link to comment
Share on other sites

Thanks kicken,

 

Testing it as we speak.  The array method goes around twice as fast (but it is still like instant).

 

I am also curious on how much memory they both use.  I know it probably doesn't matter, but as I said, just curious.

 

I've never checked real time memory usage, and if you have a pointer, it would be appreciated.  If not, I am sure I will find something.

 

EDIT.  Took 2 seconds to google: http://php.net/manual/en/function.memory-get-usage.php

 

Thanks

Edited by NotionCommotion
Link to comment
Share on other sites

An array has the least syntax noise and reflects the idea of mapping keys to paths, so I'd use that.

 

// Don't bother about micro-optimizations (or rather nano-optimizations in this case). I can assure you that there are much, much worse bottlenecks lurking somewhere in your application.

Edited by Jacques1
Link to comment
Share on other sites

I'm not worried about micro-optimizations, just curious.  Do you know how to interpret memory usage?

 

Using a switch statement:

Time: 0.0052700042724609
memory_get_peak_usage(): 136328
memory_get_peak_usage(true): 262144
memory_get_usage(): 134860
memory_get_usage(true): 262144

Using an array:

Time: 0.0024628639221191
memory_get_peak_usage(): 137304
memory_get_peak_usage(true): 262144
memory_get_usage(): 136200
memory_get_usage(true): 262144
Link to comment
Share on other sites

The array lookup method would be best if you can define it just once, but reference it multiple times. Either define it in a class constructor, or if this function is standalone you could use a static variable and initialize it on the first function call.

 

function getPath($path){
   static $map;
   if (!$map){
      //define array
   }

   if (isset($map[$path])) return $map[$path];
   throw new Exception("Path {$path} not valid.");
}
Edited by kicken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.