Solved

performance of !is_null and is_null in function

Posted on 2003-11-17
5
362 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 350 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now