1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Which consumes more execution time in_array or foreach?

Discussion in 'PHP' started by HussainMHB, May 2, 2013.

  1. #1
    I want to reduce my project execution time as tiny as possible.
    Which is perfect for reducing time

    In_array or foreach

    Please let me know and i'll be waiting for the solutions.
    HussainMHB, May 2, 2013 IP
  2. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #2
    in_array

    When you use foreach loop, PHP will process code.
    But when you call in_array the C compiled source will do this. So processing binaries is faster than scripting languages.
    Hamidsam, May 2, 2013 IP
  3. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #3
    You shouldn't care about all these micro-optimizations. If you really want to find out the bottlenecks of your application, you should use a profiler.
    PHP has many array functions. Use the right function for the right job.
    Last edited: May 2, 2013
    gvre, May 2, 2013 IP
    sarahk and ryan_uk like this.
  4. ThePHPMaster

    ThePHPMaster Active Member

    Messages:
    596
    Likes Received:
    30
    Best Answers:
    18
    Trophy Points:
    75
    #4
    If you have to, in_array. Personally I prefer neither. If your array is sorted, using a binary search you can achieve most optimal speeds average speed of (O(log N)).

    +1 for profiler. If you ever get to work with applications that are already heavily optimized and serve thousands of requests per second, these optimization will make a difference (usually I use the profiler to see what is the slowest functions and how they can be switched with different functions/logic that will make it faster).
    ThePHPMaster, May 2, 2013 IP
  5. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #5
    I know. That's why I suggested the profiler (xdebug).
    gvre, May 4, 2013 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    17,407
    Likes Received:
    1,740
    Best Answers:
    37
    Trophy Points:
    510
    #6
    I used to fret about the difference between strings defined with "quotes" or 'quotes' and, yes, there is a difference. However once you've worked on a really big system like vBulletin and see how many queries, how much code gets run you'll realise your own app is probably small fry by comparison.

    You'll probably make bigger gains by ensuring your database is appropriately indexed, your code is well structured and there aren't redundant loops or obsolete code.
    sarahk, May 4, 2013 IP
  7. annaharris

    annaharris Member

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    I always use in_array to reduce execution time.
    annaharris, May 6, 2013 IP