美文网首页编程珠玑 Pearls 第二版
编程珠玑-续1 Pearls C/C++性能监测工具

编程珠玑-续1 Pearls C/C++性能监测工具

作者: 百炼 | 来源:发表于2016-11-27 09:56 被阅读0次

    参考

    1. gprof、gprof2dot.py、dot使用方法简介

    2. gcov、lcov与genhtml 使用心得

    3. Linux平台代码覆盖率测试工具GCOV的前端工具LCOV简介

    4. NetBeans C/C++远程开发环境


    makefile文件留记

    文件及目录

    $ ls
    descriptions.txt  Makefile  methods  p1.c  prime.h  README
    $ls methods/
    prime.c
    

    Makefile 根据3. Linux平台代码覆盖率测试工具GCOV的前端工具LCOV简介 中lcov自带例子中的makefile修改。

    p4.c

    #include<stdio.h>
    #include<math.h>
    int root (int n)
    {
      return (int) sqrt ((float) n);
    }
    int prime (int n)
    {
      int i, bound;
      if (n % 2 == 0)
        return (n == 2);
      if (n % 3 == 0)
        return (n == 3);
      if (n % 5 == 0)
        return (n == 5);
      if (n % 7 == 0)
        return (n == 7);
      if (n % 11 == 0)
        return (n == 11);
      bound = root (n);
      for (i = 13; i <= bound; i=i+2)
        if (n % i == 0)
          return 0;
      return 1;
    }
    int main (int argc, char *argv[])
    {
      int i, n;
      if (argc == 2)
        {
          n = atoi (argv[1]);
        }
      for (i = 2; i <= n; i++)
        if (prime (i))
          printf ("%d\n", i);
      return 0;
    }
    

    根据安装的lcov调整LCOV := /usr/bin/lcov,GENHTML := /usr/bin/genhtml,GENDESC := /usr/bin/gendesc
    GENPNG := /usr/bin/genpng

    #
    # Makefile for the LCOV p4 program.
    #
    # Make targets:
    #   - p4: compile the p4 program
    #   - output:  run test cases on p4 program and create HTML output
    #   - clean:   clean up directory
    #
    
    CC      := gcc
    CFLAGS  := -Wall -I. -fprofile-arcs -ftest-coverage
    
    LCOV    := /usr/bin/lcov
    GENHTML := /usr/bin/genhtml
    GENDESC := /usr/bin/gendesc
    GENPNG  := /usr/bin/genpng
    
    # Depending on the presence of the GD.pm perl module, we can use the
    # special option '--frames' for genhtml
    USE_GENPNG := $(shell $(GENPNG) --help >/dev/null 2>/dev/null; echo $$?)
    
    ifeq ($(USE_GENPNG),0)
      FRAMES := --frames
    else
      FRAMES :=
    endif
    
    .PHONY: clean output test_1000
    
    all: output
    
    p4: p4.o
        $(CC) p4.o -o p4 -lgcov -lm
    
    p4.o: p4.c
        $(CC) $(CFLAGS) -c p4.c -o p4.o
    
    output: p4 descriptions test_1000
        @echo
        @echo '*'
        @echo '* Generating HTML output'
        @echo '*'
        @echo
        $(GENHTML) trace_args.info \
               --output-directory output --title "Basic p4" \
               --show-details --description-file descriptions $(FRAMES) \
               --legend
        @echo
        @echo '*'
        @echo '* See '`pwd`/output/index.html
        @echo '*'
        @echo
    
    descriptions: descriptions.txt
        $(GENDESC) descriptions.txt -o descriptions
    
    all_tests: test_1000
    
    test_1000:
        @echo '*'
        @echo '* Test case 1: running ./p4 100'
        @echo '*'
        @echo
        $(LCOV) --zerocounters --directory .
        ./p4 1000
        $(LCOV) --capture --directory . --output-file trace_args.info --test-nam
    e "test_1000" --no-externalclean:
        rm -rf *.o *.bb *.bbg *.da *.gcno *.gcda *.info output p4 \
        descriptions
    

    相关文章

      网友评论

        本文标题:编程珠玑-续1 Pearls C/C++性能监测工具

        本文链接:https://www.haomeiwen.com/subject/ghzgpttx.html