DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
  • Never Use Credentials in a CI/CD Pipeline Again
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
  • Never Use Credentials in a CI/CD Pipeline Again
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  1. DZone
  2. Coding
  3. Languages
  4. iconv_substr vs mbstring_substr in PHP

iconv_substr vs mbstring_substr in PHP

Evert Pot user avatar by
Evert Pot
·
Oct. 04, 11 · News
Like (0)
Save
Tweet
Share
5.24K Views

Join the DZone community and get the full member experience.

Join For Free

While working on an application I ran across a huge bottleneck which I isolated down all the way to the use of the iconv_substr function. If you ever wonder which is better to use, this should help your decision:

Benchmark script

    <?php
     
    $str = str_repeat("foo",100000);
    $time = microtime(true);
     
    iconv_substr($str,1000,90000,'UTF-8');
     
    echo "iconv_substr: " . (microtime(true)-$time) . "\n";
     
    $time = microtime(true);
     
    mb_substr($str,1000,90000,'UTF-8');
     
    echo "mb_substr: " . (microtime(true)-$time) . "\n";
     
    $time = microtime(true);
     
    substr($str,1000,90000);
     
    echo "substr: " . (microtime(true)-$time) . "\n";
    ?>

The results widely varied between machines, operating systems and PHP versions; but here are two results I recorded.

First, PHP 5.3.4 on OS/X:

    iconv_substr: 0.014400005340576
    mb_substr: 0.00049901008605957
    substr: 3.7193298339844E-5 # Note the E-notation, this was actually something like 0.00003 seconds.

As you can see iconv took 0.01 seconds, while mbstring took only 0.0004 seconds. Already a significant difference (2800% slower), but the difference became more apparent when running this on a Debian box with PHP 5.2.13.

    iconv_substr: 8.3735179901123
    mb_substr: 0.00039505958557129
    substr: 4.8160552978516E-5

Yup, it took 8.3 seconds. That's an increase of over 2100000%. So next time you're wondering which of the two may be smarter to use, this may help you decide.

Important to note that OS/X uses libiconv as the 'iconv implementation' and my Debian test machine 'glibc', so it looks like libiconv is much, much faster than glibc. mbstring still leaves both in the dust though.

I'm interested to hear what your results are, especially if they differ.

PHP

Published at DZone with permission of Evert Pot, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
  • Never Use Credentials in a CI/CD Pipeline Again
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: