473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Efficient Code or Not?

eWish
971 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. my ($file_to_modify) = &set_log_file(3);
  2. my (@act_valid_log) = &read_data_file(3);
  3.  
  4. #do something with the result of @act_valid_log 
  5.  
  6.  
  7. sub set_log_file {
  8.  
  9.     my ($get_path) = @_;
  10.  
  11.     my ($return_path)     = $get_path == 1 ? qq{giftcard_log.log}
  12.                         : $get_path    == 2 ? qq{giftcard_db.log}
  13.                         : $get_path    == 3 ? qq{giftcard_act_valid.log}
  14.                         :                   &errorcode(__FILE__, __LINE__, "Your entry does not match anything.", "$!");    
  15.                         ;
  16.  
  17.     return ($return_path);
  18.  
  19. }
  20.  
  21. sub read_data_file {
  22.  
  23.     my ($file_to_read) = @_;
  24.     my ($giftcard_file) = &set_log_file($file_to_read);
  25.     my (@data);
  26.  
  27.         open(my $READ_DATA_FILE, '<', $giftcard_file) || &errorcode(__FILE__, __LINE__, $giftcard_file, "$!");    
  28.         @data = <$READ_DATA_FILE>;
  29.         close($READ_DATA_FILE);
  30.  
  31.     return(@data);
  32.  
  33. }
Looking for some feedback.

--Kevin
Dec 31 '07 #1
10 1394
KevinADC
4,059 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. my ($file_to_modify) = &set_log_file(3);
  2. my (@act_valid_log) = &read_data_file(3);
  3.  
  4. #do something with the result of @act_valid_log 
  5.  
  6.  
  7. sub set_log_file {
  8.  
  9.     my ($get_path) = @_;
  10.  
  11.     my ($return_path)     = $get_path == 1 ? qq{giftcard_log.log}
  12.                         : $get_path    == 2 ? qq{giftcard_db.log}
  13.                         : $get_path    == 3 ? qq{giftcard_act_valid.log}
  14.                         :                   &errorcode(__FILE__, __LINE__, "Your entry does not match anything.", "$!");    
  15.                         ;
  16.  
  17.     return ($return_path);
  18.  
  19. }
  20.  
  21. sub read_data_file {
  22.  
  23.     my ($file_to_read) = @_;
  24.     my ($giftcard_file) = &set_log_file($file_to_read);
  25.     my (@data);
  26.  
  27.         open(my $READ_DATA_FILE, '<', $giftcard_file) || &errorcode(__FILE__, __LINE__, $giftcard_file, "$!");    
  28.         @data = <$READ_DATA_FILE>;
  29.         close($READ_DATA_FILE);
  30.  
  31.     return(@data);
  32.  
  33. }
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.
Dec 31 '07 #2
eWish
971 Expert 512MB
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
Dec 31 '07 #3
Kelicula
176 Expert 100+
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".

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $var = &someSub(4);
  4.  
  5. sub someSub {
  6.  
  7. my $temp = shift; #grabs the first, (and only) value in the @_ array "4".
  8.  
  9. ...do something...
  10.  
  11. return something;
  12. }
  13.  
Jan 1 '08 #4
eWish
971 Expert 512MB
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
Jan 1 '08 #5
Kelicula
176 Expert 100+
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.
Jan 1 '08 #6
KevinADC
4,059 Expert 2GB
You seem to have a parenthesis fetish:

Expand|Select|Wrap|Line Numbers
  1. my ($file_to_read) = @_;
  2. my ($giftcard_file) = &set_log_file($file_to_read);
  3. 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.
Jan 1 '08 #7
eWish
971 Expert 512MB
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
Jan 2 '08 #8
KevinADC
4,059 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. print "$foo";
Jan 2 '08 #9
Kelicula
176 Expert 100+
Try Benchmarking the script.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl -w
  3.  
  4. use strict;
  5.  
  6. use Benchmark;
  7.  
  8. my $t = new Benchmark;
  9.  
  10. the code....
  11.  
  12. my $t2 = new Benchmark;
  13.  
  14. my $td = timediff($t2, $t);
  15.  
  16. print timestr($td);
  17.  
  18.  
See how long it takes to run, with then without p's.

Curious....
Jan 2 '08 #10
Kelicula
176 Expert 100+
I hear the switch operator is more efficient with numeric equalities.

eg:

Expand|Select|Wrap|Line Numbers
  1.  
  2. use Switch;
  3.  
  4. switch ($val) {
  5.     case 1        { print "number 1" }
  6.     case "a"    { print "string a" }
  7.     case [1..10,42]    { print "number in list" }
  8.     case (@array)    { print "number in list" }
  9.     case /\w+/    { print "pattern" }
  10.     case qr/\w+/    { print "pattern" }
  11.     case (%hash)    { print "entry in hash" }
  12.     case (\%hash)    { print "entry in hash" }
  13.     case (\&sub)    { print "arg to subroutine" }
  14.     else        { print "previous case not true" }
  15.     }
  16.  
See this... Switch
Feb 5 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
3
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...
3
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...
8
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...
5
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...
74
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++...
1
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...
28
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
3
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...
25
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...
0
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...
1
isladogs
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...
0
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...
0
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...
1
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.