?
Solved

performance of !is_null and is_null in function

Posted on 2003-11-17
5
Medium Priority
?
374 Views
Last Modified: 2013-12-13
With the following function I'm wondering would there be a performance difference using is_null return $result else
$result = str_pad($value, $pad_length,$pad_string,STR_PAD_BOTH);
return $result;
In pl/sql I was told not to use the not in or is not equal to operators as there was more overhead.  This function will be used on 100K rows are more so the slightest saving will be important.
Tanks in advance.

<?php
both_str_pad($pad_length,$pad_string,$value)
{  
    if (!is_null($value)
    {//IF ONE
   
    $result = str_pad($value, $pad_length,$pad_string,STR_PAD_BOTH);
   
    return $result;
   
    }//END IF ONE
    else
    {//ELSE ONE
     
    return $result;
     
    }//END ELSE ONE

}
?>
0
Comment
Question by:dplinnane
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:knel1234
ID: 9765768
dplinnane,

The reason you were told not to use the "not in" or "not equal" in PL/SQL is that it often (if not always) creates a VERY expensive query.  Most DBAs as well as PL/SQL developers find there biggest performance hits from correcting this type of implementation.  The amount of logical I/Os that Oracle(sybase, sqlserver, etc) must do on a "not in" (you can also think of this as several "OR"s) on a table of 100K can quickly get into the millions when join to other table(s).  You can often solve this DB issue be restating you logic (and therefore the "where clause") as well as using a "decode" in the select.

I dont think your PHP is costing you all that much here.  If you truely have to check each and every row which you seem to be indicating that you do, then you will have to call this function for each row of data.  Someone might try to claim that you are using extra calls or bytes etc etc. but this will most likely not be your bottleneck.

The two "not in" concepts dont really have the same purpose or implications.  
99% of the time a "not in" in SQL is incorrect.  However, most of us that create cleints of software to massage data need to ensure that the data is correct and/or with not effect our displays.  Therefore, it wouldnt surprise me if you need to call this function 100K times.


ALL THAT being said....
If this code is meant for 100K rows are these rows from a DB table, flat file, or other.
If they are from a flatfile, then so be it... go through 100K rows with this function.
If they are from a DB file, then why dont you use a "decode" (this is an oracle function but most DB have a similar one) and set nulls equal to a specific value like:       0, "", "xxxxxx", "       ", or the like.
You are already touching the row and therefore the fielding during the select process so why not handle it there.  Hopefully, this will allow your PHP code to ASSUME correct values and therefore not have to call a function to ensure proper data.

As a further thought.....
Should this field in the DB allow nulls and/or be defaulted to something...
This is something to discuss with your DBA and/or the people more involved with your product/code.

Let me know if this helps....

knel
0
 

Author Comment

by:dplinnane
ID: 9766048
This definitely helps.
I will always be processing csv files.  The main reason I want to use is_null is incase I have a complicated function
that may use unnecessary cpu time if called, so if is_null skip it and save cpu time. Another question in relation to performance.
Which has more overhead and why  nesting functions like
$result = f3(f2(f1($val1)));

or
$result = f1($val1);
$result = f2($result );
$result = f3($result );

Any other performance increasing tips would be useful.
Input file will always be in.csv and out file out.csv

Thx I added an extra 100 points.
0
 
LVL 3

Expert Comment

by:knel1234
ID: 9767293
dplinnane,

are f1,f2,f3....
require, included, or inline code???
0
 

Author Comment

by:dplinnane
ID: 9767811
I have a file called clean_string.php

I have a setup.php file which contains all of my included files.
define(CLEAN_STRING, $_SERVER[DOCUMENT_ROOT]."$path/clean_string.php");
include_once(CLEAN_STRING);

Within clean_string.php I have a bunch of functions for formatting strings.
0
 
LVL 3

Accepted Solution

by:
knel1234 earned 1400 total points
ID: 9771687
dplinnane,

I dont think it makes a difference.  The machine will need to allocated/destroy the variable in a similar manner to the ones you are declaring.

I would do the following:
$r1=f1($result);
$r2=f2($r1);
$r3=f3($r2);

I just think its will be easier to read and debug 3 months from now.
Especially, if you want/need to "echo" or "print" something.



knel
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question