NotionCommotion Posted December 29, 2014 Share Posted December 29, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/293502-get-string-based-on-another-string/ Share on other sites More sharing options...
kicken Posted December 29, 2014 Share Posted December 29, 2014 Use whichever method you find most readable. The performance difference between the two is probably negligible (you could test it if your concerned/curious). Quote Link to comment https://forums.phpfreaks.com/topic/293502-get-string-based-on-another-string/#findComment-1501069 Share on other sites More sharing options...
NotionCommotion Posted December 29, 2014 Author Share Posted December 29, 2014 (edited) 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 December 29, 2014 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/293502-get-string-based-on-another-string/#findComment-1501071 Share on other sites More sharing options...
Jacques1 Posted December 29, 2014 Share Posted December 29, 2014 (edited) 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 December 29, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/293502-get-string-based-on-another-string/#findComment-1501072 Share on other sites More sharing options...
NotionCommotion Posted December 29, 2014 Author Share Posted December 29, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/293502-get-string-based-on-another-string/#findComment-1501073 Share on other sites More sharing options...
kicken Posted December 29, 2014 Share Posted December 29, 2014 (edited) 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 December 29, 2014 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/293502-get-string-based-on-another-string/#findComment-1501076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.