Will this style of coding cause me some speeding penalties? I am just trying to reduce the number of times I have to write an open function. Naturally, this is just an example. - my ($file_to_modify) = &set_log_file(3);
-
my (@act_valid_log) = &read_data_file(3);
-
-
#do something with the result of @act_valid_log
-
-
-
sub set_log_file {
-
-
my ($get_path) = @_;
-
-
my ($return_path) = $get_path == 1 ? qq{giftcard_log.log}
-
: $get_path == 2 ? qq{giftcard_db.log}
-
: $get_path == 3 ? qq{giftcard_act_valid.log}
-
: &errorcode(__FILE__, __LINE__, "Your entry does not match anything.", "$!");
-
;
-
-
return ($return_path);
-
-
}
-
-
sub read_data_file {
-
-
my ($file_to_read) = @_;
-
my ($giftcard_file) = &set_log_file($file_to_read);
-
my (@data);
-
-
open(my $READ_DATA_FILE, '<', $giftcard_file) || &errorcode(__FILE__, __LINE__, $giftcard_file, "$!");
-
@data = <$READ_DATA_FILE>;
-
close($READ_DATA_FILE);
-
-
return(@data);
-
-
}
Looking for some feedback.
--Kevin
10 1394
Will this style of coding cause me some speeding penalties? I am just trying to reduce the number of times I have to write an open function. Naturally, this is just an example. - my ($file_to_modify) = &set_log_file(3);
-
my (@act_valid_log) = &read_data_file(3);
-
-
#do something with the result of @act_valid_log
-
-
-
sub set_log_file {
-
-
my ($get_path) = @_;
-
-
my ($return_path) = $get_path == 1 ? qq{giftcard_log.log}
-
: $get_path == 2 ? qq{giftcard_db.log}
-
: $get_path == 3 ? qq{giftcard_act_valid.log}
-
: &errorcode(__FILE__, __LINE__, "Your entry does not match anything.", "$!");
-
;
-
-
return ($return_path);
-
-
}
-
-
sub read_data_file {
-
-
my ($file_to_read) = @_;
-
my ($giftcard_file) = &set_log_file($file_to_read);
-
my (@data);
-
-
open(my $READ_DATA_FILE, '<', $giftcard_file) || &errorcode(__FILE__, __LINE__, $giftcard_file, "$!");
-
@data = <$READ_DATA_FILE>;
-
close($READ_DATA_FILE);
-
-
return(@data);
-
-
}
Looking for some feedback.
--Kevin
It will be slightly more efficient if you just use one subroutine. set_log_file looks like it could be part of read_data_file instead of a seperate sub. But if you have other subs that call set_log_file that would be OK. Overall, there is nothing really inefficient about the code that I see. Personally I don't like to write a single scalar as a list assignment:
my ($file_to_read) = @_;
I would write that as:
my $file_to_read = $_[0];
although I am not sure if it is anymore efficient one way or the other, it's really a matter of personal preference I guess.
Kevin Thanks for your comments and suggestions. I will take your advice on the $_[0] over @_ for a single value. That is a good idea.
--Kevin
Kevin Thanks for your comments and suggestions. I will take your advice on the $_[0] over @_ for a single value. That is a good idea.
--Kevin
Also if you are always going to be sending only one parameter to the sub, you can use "shift". -
#!/usr/bin/perl
-
-
my $var = &someSub(4);
-
-
sub someSub {
-
-
my $temp = shift; #grabs the first, (and only) value in the @_ array "4".
-
-
...do something...
-
-
return something;
-
}
-
Kelicula,
I am trying to keep the coding style as close to the existing code as I can. Currently it does not use the shift style of code.
--Kevin
Kelicula,
I am trying to keep the coding style as close to the existing code as I can. Currently it does not use the shift style of code.
--Kevin
Oh, I see.
Now I understand. I was wondering when I replied before, cause I know you've used shift before.
OK.
You seem to have a parenthesis fetish: - my ($file_to_read) = @_;
-
my ($giftcard_file) = &set_log_file($file_to_read);
-
my (@data);
none of the above lines needs the parenthesis on the left side, well the first one does but we already discussed that one. Is it just habit? It would be interesting to see if it affects efficiency or not. Bascially you are creating lists with one element. I wonder if perl has to take any extra steps to first make a list then make the assignment to the elements in the list. Maybe someone on perlmongers would know.
I thought that a fetish with the P word parentheses was better that a fetish with the P word that ends in HP. :) Nonetheless, it is a habit and I have removed them.
Also, I decided to use a hash of named arguments. Which deviates form the original code. Then I used a hash for the log files in lieu of a subroutine.
--Kevin
I thought that a fetish with the P word parentheses was better that a fetish with the P word that ends in HP. :) Nonetheless, it is a habit and I have removed them.
Also, I decided to use a hash of named arguments. Which deviates form the original code. Then I used a hash for the log files in lieu of a subroutine.
--Kevin
I don't know if the parentheses usage is really a concern or not. I see other perl scripts written that way and I think it is probably habit or just style preference. It could be much ado about nothing so if you prefer writing your code that way I would say continue to do so.
In some situations the parentheses are critical, but in the code you posted they are not. I just wonder if it causes any extra work for perl, like quoting scalars when they don't need quoting:
Try Benchmarking the script. -
-
#!/usr/bin/perl -w
-
-
use strict;
-
-
use Benchmark;
-
-
my $t = new Benchmark;
-
-
the code....
-
-
my $t2 = new Benchmark;
-
-
my $td = timediff($t2, $t);
-
-
print timestr($td);
-
-
See how long it takes to run, with then without p's.
Curious....
I hear the switch operator is more efficient with numeric equalities.
eg: -
-
use Switch;
-
-
switch ($val) {
-
case 1 { print "number 1" }
-
case "a" { print "string a" }
-
case [1..10,42] { print "number in list" }
-
case (@array) { print "number in list" }
-
case /\w+/ { print "pattern" }
-
case qr/\w+/ { print "pattern" }
-
case (%hash) { print "entry in hash" }
-
case (\%hash) { print "entry in hash" }
-
case (\&sub) { print "arg to subroutine" }
-
else { print "previous case not true" }
-
}
-
See this... Switch Sign in to post your reply or Sign up for a free account.
Similar topics
by: Linus Nikander |
last post by:
Having recently load-tested the application we are developing I noticed that
one of the most expensive (time-wise) calls was my fetch of a db-connection
from the defined db-pool. At present I fetch...
|
by: Chris Tanger |
last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
|
by: sandeep |
last post by:
Hi i am new to this group and to c++ also though i have the knowledge
of "c" and now want to learn c++ and data structure using c/c++ .
so could nebody please suggest me some...
|
by: brian.digipimp |
last post by:
I turned this in for my programming fundamentals class for our second
exam. I am a c++ newb, this is my first class I've taken. I got a good
grade on this project I'm just wondering if there is a...
|
by: Alan Little |
last post by:
I have affiliates submitting batches of anywhere from 10 to several
hundred orders. Each order in the batch must include an order ID,
originated by the affiliate, which must be unique across all...
|
by: copx |
last post by:
In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that
properly written C++ outperforms C code. I will just copy his first example
here, which is supposed to demonstrate how C++...
|
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= |
last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus
spreading them across multiple folders.
For instance if we have 100,000 files to be copied,
Do we copy all of them to...
|
by: Mahesh |
last post by:
Hi,
I am looking for efficient string cancatination c code. I did it using
ptr but my code i think is not efficient.
Please help.
Thanks a lot
|
by: Ken Fine |
last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets
and DataTables should be able to answer fairly easily. The basic question is
how I can efficiently match data from one...
|
by: Abubakar |
last post by:
Hi,
recently some C programmer told me that using fwrite/fopen functions
are not efficient because the output that they do to the file is
actually buffered and gets late in writing. Is that...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |