Forum Moderators: coopster & phranque

Message Too Old, No Replies

Is it faster to use a module than to roll your own?

         

csdude55

8:10 pm on Nov 28, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I use the JSON module, but only use the decode_json function:

[metacpan.org...]

The .tar.gz download is 92kb, but I'm not sure how big the uncompressed file is.

I use it like:

use JSON;

$jsonArr = false;
if ($json_string) {
eval { $jsonArr = decode_json $json_string };
}

if ($jsonArr) {
for (@{$jsonArr}) {
$pic = $_->{'name'};
$uuid = $_->{'uuid'};

# do stuff
}
}


As an alternative, I could accomplish the same thing with:

sub myJSONDecoder {
my @finalArr;
$_ = $_[0];

s/^\s*\[\s*{\s*|\s*}\s*\]\s*$//g;

my @firstArr = split(/\s*}\s*,\s*{/);

for ($i = 0; $i <= $#firstArr; $i++) {
my @secondArr = split(/\s*,\s*/, $firstArr[$i]);

for (@secondArr) {

# I recognize that this next regex would break if there's a ' inside of a " or
# vice versa. That's not an issue for my usage, but YMMV
s/["']//g;

my @thirdArr = split(':');

$finalArr[$i]{$thirdArr[0]} = $thirdArr[1];
}
}

return @finalArr;
}

@jsonArr = myJSONDecoder($json_string);

for (@jsonArr) {
$pic = $_->{'name'};
$uuid = $_->{'uuid'};

# do stuff
}


That's 377 bytes instead of 92,000. Or more like 296 if I change the variable names strategically, I just used explanatory names to make it more understandable for this post.

Is there a reason to stick with the module? I know that 4 expressions in my function would be a tad slow to process, but does that offset the time it would take to load the much larger module that, for all I know, may have its own expressions?

Are modules by nature faster to process? Or is it just that it will be more likely to be stable?

csdude55

8:16 pm on Dec 5, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Similar but different...

I have a few functions that I use in multiple scripts, so I have them in a separate file that I include using require foo.lib;.

But I've read some posts (mostly much older) saying that "require" is very slow, and it's better to use a module.

Would it be advantageous to modify "foo.lib" to include package Foo; at the top, rename it to Foo.pm, then move it to /usr/local/lib64/perl5 (where my modules are stored)? Then modify the scripts to "use Foo;", of course.

Am I right that this is all that it takes to convert it to a module? If so, is there a benefit to doing this that offsets making it slightly harder to maintain?

phranque

11:12 pm on Dec 5, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I have a few functions that I use in multiple scripts, so I have them in a separate file that I include using require foo.lib;.

But I've read some posts (mostly much older) saying that "require" is very slow, and it's better to use a module.

require happens at run time.
use happens at compile time.
either can be used to load a module, according to requirements.

Am I right that this is all that it takes to convert it to a module?

you can learn all you need to know about perl modules in the perlod man page [manpages.org]

[edited by: phranque at 3:08 am (utc) on Dec 6, 2022]

phranque

11:21 pm on Dec 5, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Or more like 296 if I change the variable names strategically,

the savings from shortening variable names is practically immeasurable.

Are modules by nature faster to process? Or is it just that it will be more likely to be stable?

compiling the same code costs about the same, no matter how the software is structured.
the main advantages of modules are reusability, maintenance of variable namespaces, and use as class definitions.

Brett_Tabke

3:57 pm on Dec 15, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I think the real beauty of hard coding it yourself is the benefit of continued compatibility and availability. I can not count the number of times I have had to track down some obscure module or realize that the author had deprecated it in favor of another module that uses different syntax. Even things you think are no-brainers can end up disappearing and leaving you scrambling for a solution (like cgi.pm). If I can hard code it - I do hard code it.

Sgt_Kickaxe

6:30 pm on Dec 15, 2022 (gmt 0)



I hate Wordpress for much the same reason, if I can go full static without a CMS, I will.

Roll your own, see how it works out and let us know. Doing for yourself is a great way to learn and become less dependent on others.

tangor

10:06 am on Dec 18, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hand rolled also has the benefit of you knowing WHAT'S IN THE CODE compared to what is hidden (unused). Easier to debug if that becomes necessary!

explorador

3:44 am on May 8, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I prefer my own code. Yes, modules are fine, but sometimes there is extra code you don't need, so there is some loss of efficiency (depending how good your code is).

But other times it's about independence... as not all servers have all the required modules installed, sometimes there are problems installing those, so... in my own experience: the less modules the better.