Table of Contents
Comments
Related posts:
Home php教程 php手册 一个小玩意PHP-Valgrind的介绍

一个小玩意PHP-Valgrind的介绍

Jun 06, 2016 pm 08:10 PM
introduce author

作者: Laruence( ) 本文地址: http://www.laruence.com/2013/08/14/2899.html 转载请注明出处 首先, 太久不更新博客了, 容我先啰嗦几句. 我一直以为人总是会挤出时间写博客的, 但现在看来我错了. 博客很久不更新, 倒不是说没有内容可以分享. 而是这一年来确

  • 作者: Laruence(一个小玩意PHP-Valgrind的介绍 一个小玩意PHP-Valgrind的介绍 一个小玩意PHP-Valgrind的介绍 一个小玩意PHP-Valgrind的介绍)
  • 本文地址: http://www.laruence.com/2013/08/14/2899.html
  • 转载请注明出处

首先, 太久不更新博客了, 容我先啰嗦几句.

我一直以为人总是会挤出时间写博客的, 但现在看来我错了. 博客很久不更新, 倒不是说没有内容可以分享. 而是这一年来确实忙了很多, 本身工作上的事情就很多, 业余时间也被PHP项目上的事情, Zend的事情填满. 再加上一些小感悟也都在微博上牢骚了… 所以….

Anyway, 很感谢各位经常来我博客的朋友, 不过我建议大家如果是提问的话, 不要在留言里, 有的时候会被博客当做SPAM, 有一些小问题, 可以来微博At我 @laruence

言归正传, 今天分享个前天刚刚做的小工具, 代码可以在我的github上找到: php-valgrind. 这个工具主要是为PHP脚本提供了可以在脚本中开启Valgrind(严格说是Callgrind)的Profile能力.

一般来说, 我们用Callgrind的时候, 如果要分析某一个函数, 可以通过toggle-collect=”函数名”, 来告诉Callgrind在进入这个函数的时候开始Profile. 但是这样没有办法分析具体某一段代码..

其实Callgrind提供了一个机制, 可以让我们在代码中控制何时开启: CALLGRIND_TOGGLE_COLLECT

比如:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include <stdio.h>

#include <valgrind>

 

void foo(int a[100]) {

    int i = 0;

}

 

int main (int argc, char **argv) {

    int i, a[100];

 

    CALLGRIND_START_INSTRUMENTATION;

    CALLGRIND_TOGGLE_COLLECT;

    for (i=0; i

<p>然后, 编译成a.out以后, 我们就可以分析具体的这个循环代码断的相关Profile信息:</p>

<pre class="brush:php;toolbar:false">

$ valgrind --tool=callgrind --collect-atstart=no --instr-atstart=no ./a.out

Copy after login

之后生成的callgrind.out就可以被callgrind_annotate, kcachegrind等工具来分析了

1

2

3

4

5

6

7

8

9

10

11

$callgrind_annotate callgrind.out.27538

//OUTPUT:

--------------------------------------------------------------------------------

 Ir

---------------------------------------

617  PROGRAM TOTALS

 

---------------------------------------

 Ir  file:function

---------------------------------------

617  test.c:main [***dev/a.out]

Copy after login

是不是很方便呢?

但是呢, 有的时候, 比如我们做扩展, 或者其他的一类内部的性能分析的时候. 需要在PHP脚本也能做这样的触发. 就没有办法了. 于是我就写了这个小工具php-valgrind, 装好这个扩展以后, 来看个例子:

1

2

3

4

5

6

<?php $a = array();

callgrind_toggle();

for ($i=0;$i<1000;$i++) {

    $a[$i] = 2;

}

callgrind_toggle();

Copy after login

然后我们开始分析:

1

$valgrind --tool=callgrind --collect-atstart=no --instr-atstart=no php /tmp/1.php

Copy after login

然后我们分析下输出:

1

2

3

4

5

6

-------------------------

       Ir

--------------------------

2,361,260  PROGRAM TOTALS

 

//以下省略

Copy after login

然后让我们用qcachegrind(带gui的callgrind分析工具), 来看看:
callgrind.output

可见, PHP要实现同样的功能需要的各种代码数相比C语言来说, 那可是多了N倍的(所以当然要比C慢了.. 嘿嘿, 再次申明: “C语言是最好的语言, 没有之一!”)

好了工具介绍完毕, 大家有兴趣的可以去玩玩, 这个工具还可以用来让我们了解, 我们的一个PHP代码, 会触发调用那些底层的函数, 或者系统调用等等, Enjoy~


Comments

  • 2013/08/14, 秋风 writes: 终于更新了,试试!
  • 2013/08/14, 夜丶有雪 writes: 前排留名、广告位招租 C语言是最好的语言, 没有之一!
  • 2013/08/14, pakey writes: 没有抢到沙发
  • 2013/08/14, 微历史 writes: 保持更新很重要
  • 2013/08/14, 风逐蓝天 writes: PHP 是最好的语言~~~
  • 2013/08/14, php230 writes: V5
  • 2013/08/14, 我叫张大熊 writes: 微博私信问了你一个问题 可是还没有得到回答 求回答啊
  • 2013/08/14, 我叫张大熊 writes: 微博私信问了你一个问题 可是还没有得到回答 求回答啊
  • 2013/08/15, eagle writes: 需要另行安装Callgrind吗?
  • 2013/08/15, eagle writes: 需要另行安装Callgrind吗?
  • 2013/10/14, wclssdn writes: 我也在微博问过大牛好几个问题.. 就是不理我... 汗... 还有. 类似大牛的这个工具可以用xhprof.. 有图片展示方式... 太直观了... 数据表格也更丰富...
  • 2013/12/18, goodboy writes: “C语言是最好的语言” ......
  • 2014/02/19, Winona writes: I was curious if you ever thought of changing the page layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two pictures. Maybe you could space it out better?
  • 2014/02/27, real estate preview writes: ?e're a ?roup ?f volunteers and starting a brand new scheme in ?ur community. Your web site provided us with u?еful information to work on. You have done a formidable job and ouг whole gro?p can be thankful to you. ?ere i? my blog: real estate preview
  • 2014/09/10, computer repairs, apple mac pro reviews, cell phone repair, computer support,mercadolibre writes: Its 23-inch screen is already written on Apple's website because "Discontinued. As a result, the smaller sized or thinner the notebook, the lesser the capabilities in view that they are pressured to go out some ports. Feel free to visit my web blog: computer repairs, apple mac pro reviews, cell phone repair, computer support,mercadolibre
  • 2014/10/17, Nigel writes: 模板大神而来~~~~~

  • 注意PHP对字符串的递增运算
  • GCC优化引起的一个”问题”

Copyright © 2010 风雪之隅 版权所有, 转载务必注明. 该Feed只供个人使用, 禁止未注明的转载或商业应用. 非法应用的, 一切法律后果自负. 如有问题, 可发E-mail至my at laruence.com.(Digital Fingerprint: 73540ba0a1738d7d07d4b6038d5615e2)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed introduction to what wapi is Detailed introduction to what wapi is Jan 07, 2024 pm 09:14 PM

Users may have seen the term wapi when using the Internet, but for some people they definitely don’t know what wapi is. The following is a detailed introduction to help those who don’t know to understand. What is wapi: Answer: wapi is the infrastructure for wireless LAN authentication and confidentiality. This is like functions such as infrared and Bluetooth, which are generally covered near places such as office buildings. Basically they are owned by a small department, so the scope of this function is only a few kilometers. Related introduction to wapi: 1. Wapi is a transmission protocol in wireless LAN. 2. This technology can avoid the problems of narrow-band communication and enable better communication. 3. Only one code is needed to transmit the signal

Detailed explanation of whether win11 can run PUBG game Detailed explanation of whether win11 can run PUBG game Jan 06, 2024 pm 07:17 PM

Pubg, also known as PlayerUnknown's Battlegrounds, is a very classic shooting battle royale game that has attracted a lot of players since its popularity in 2016. After the recent launch of win11 system, many players want to play it on win11. Let's follow the editor to see if win11 can play pubg. Can win11 play pubg? Answer: Win11 can play pubg. 1. At the beginning of win11, because win11 needed to enable tpm, many players were banned from pubg. 2. However, based on player feedback, Blue Hole has solved this problem, and now you can play pubg normally in win11. 3. If you meet a pub

Introduction to Python functions: Introduction and examples of exec function Introduction to Python functions: Introduction and examples of exec function Nov 03, 2023 pm 02:09 PM

Introduction to Python functions: Introduction and examples of exec function Introduction: In Python, exec is a built-in function that is used to execute Python code stored in a string or file. The exec function provides a way to dynamically execute code, allowing the program to generate, modify, and execute code as needed during runtime. This article will introduce how to use the exec function and give some practical code examples. How to use the exec function: The basic syntax of the exec function is as follows: exec

Detailed introduction to whether i5 processor can install win11 Detailed introduction to whether i5 processor can install win11 Dec 27, 2023 pm 05:03 PM

i5 is a series of processors owned by Intel. It has various versions of the 11th generation i5, and each generation has different performance. Therefore, whether the i5 processor can install win11 depends on which generation of the processor it is. Let’s follow the editor to learn about it separately. Can i5 processor be installed with win11: Answer: i5 processor can be installed with win11. 1. The eighth-generation and subsequent i51, eighth-generation and subsequent i5 processors can meet Microsoft’s minimum configuration requirements. 2. Therefore, we only need to enter the Microsoft website and download a "Win11 Installation Assistant" 3. After the download is completed, run the installation assistant and follow the prompts to install Win11. 2. i51 before the eighth generation and after the eighth generation

Introducing the latest Win 11 sound tuning method Introducing the latest Win 11 sound tuning method Jan 08, 2024 pm 06:41 PM

After updating to the latest win11, many users find that the sound of their system has changed slightly, but they don’t know how to adjust it. So today, this site brings you an introduction to the latest win11 sound adjustment method for your computer. It is not difficult to operate. And the choices are diverse, come and download and try them out. How to adjust the sound of the latest computer system Windows 11 1. First, right-click the sound icon in the lower right corner of the desktop and select "Playback Settings". 2. Then enter settings and click "Speaker" in the playback bar. 3. Then click "Properties" on the lower right. 4. Click the "Enhance" option bar in the properties. 5. At this time, if the √ in front of "Disable all sound effects" is checked, cancel it. 6. After that, you can select the sound effects below to set and click

Introduction to edge shortcut keys Introduction to edge shortcut keys Jul 12, 2023 pm 05:57 PM

In today's fast life, in order to improve work efficiency, shortcut keys are an essential work requirement. A shortcut key is a key or key combination that provides an alternative way to perform an action normally performed using a mouse. So what are the edge shortcut keys? What are the functions of edge shortcut keys? The editor below has compiled an introduction to edge shortcut keys. Friends who are interested should come and take a look! Ctrl+D: Add the current page to favorites or reading list Ctrl+E: Perform a search query in the address bar Ctrl+F: Find on the page Ctrl+H: Open the history panel Ctrl+G: Open the reading list panel Ctrl +I: Open the favorites list panel (the test does not seem to work) Ctrl+J: Open

PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions Feb 25, 2024 am 11:15 AM

PyCharm is a powerful Python integrated development environment with rich functions and tools that can greatly improve development efficiency. Among them, the replacement function is one of the functions frequently used in the development process, which can help developers quickly modify the code and improve the code quality. This article will introduce PyCharm's replacement function in detail, combined with specific code examples, to help novices better master and use this function. Introduction to the replacement function PyCharm's replacement function can help developers quickly replace specified text in the code

Detailed information on the location of the printer driver on your computer Detailed information on the location of the printer driver on your computer Jan 08, 2024 pm 03:29 PM

Many users have printer drivers installed on their computers but don't know how to find them. Therefore, today I bring you a detailed introduction to the location of the printer driver in the computer. For those who don’t know yet, let’s take a look at where to find the printer driver. When rewriting content without changing the original meaning, you need to The language is rewritten to Chinese, and the original sentence does not need to appear. First, it is recommended to use third-party software to search. 2. Find "Toolbox" in the upper right corner. 3. Find and click "Device Manager" below. Rewritten sentence: 3. Find and click "Device Manager" at the bottom 4. Then open "Print Queue" and find your printer device. This time it is your printer name and model. 5. Right-click the printer device and you can update or uninstall it.

See all articles