Im coding in php and taking part in a coding competition which gives points to scripts on the basis of memory usage, running time, and ofcourse accuracy of algorighm.
I got the algo right and working for all test cases. But I got a little less marks than I expected.
To save lines of code, I used strpos() function in a loop.
when I changed strpos() function to manually finding string function that I made, my points increased…
Now I’m confused… I guess I can make more points if I use my own defined functions instead of all library functions I used (strlen,strpos,etc) …
Does making our own defined functions in scripts help in making the code faster ?
I’m not a professional but have worked in php for 3-4 years and never thought of saving time/memory before 😛 so I’m kinda stuck over here…
In general, the built-in functions for basic things like string operations tend to be faster than anything you could code yourself.
According to TuxRadar, built-in PHP functions use
so it’s “never better to rewrite a built-in function using PHP”.
Part of the performance issue of writing functions in PHP is that PHP code usually isn’t compiled before running, it’s interpreted. I think I read online somewhere a while back that some Facebook engineers actually wrote a PHP compiler or something though…but I could be wrong, I don’t quite remember off the top of my head.