index % time self children called name 0.00 0.00 100/100 tt [3] [1] 0.0 0.00 0.00 100 aa [1] ----------------------------------------------- 0.00 0.00 100/100 tt [3] [2] 0.0 0.00 0.00 100 bb [2] ----------------------------------------------- 0.00 0.00 100/100 main [9] [3] 0.0 0.00 0.00 100 tt [3] 0.00 0.00 100/100 aa [1] 0.00 0.00 100/100 bb [2] -----------------------------------------------
아무튼 아래와 같은 형식의 출력이 보이는데, 참 봐도 무슨 소리인지 모르겠다.
간단하게 생각하자면
[%d] 값은 함수이고, 숫자 나온 것에 대한 구조를 보여준다.
[1] 은 aa() 를 실행한 녀석이 tt() 이라는 의미이고
[2] 는 bb() 를 실행한 녀석이 tt()
[3] 는 tt()를 실행한 녀석이 main() 이라는 이야기이다.
tt()
{
aa();
bb();
}
main()
{
tt();
}
이런 구조를 지니게 된다.
만약, 하나의 함수를 여러개에서 호출한다면
위와 같이 [%d] 위에 하나씩 있는게 아니라 여러개가 나타나게 된다.
아래는 Kprof에서 실행한 결과이다.(main 함수는 어디로 가출한겨 ㄱ-)
$ gprof Flat profile:
Each sample counts as 0.01 seconds. no time accumulated
% cumulative self self total time seconds seconds calls Ts/call Ts/call name 0.00 0.00 0.00 100 0.00 0.00 aa 0.00 0.00 0.00 100 0.00 0.00 bb 0.00 0.00 0.00 100 0.00 0.00 tt
% the percentage of the total running time of the time program used by this function.
cumulative a running sum of the number of seconds accounted seconds for by this function and those listed above it.
self the number of seconds accounted for by this seconds function alone. This is the major sort for this listing.
calls the number of times this function was invoked, if this function is profiled, else blank. self the average number of milliseconds spent in this ms/call function per call, if this function is profiled, else blank.
total the average number of milliseconds spent in this ms/call function and its descendents per call, if this function is profiled, else blank.
name the name of the function. This is the minor sort for this listing. The index shows the location of the function in the gprof listing. If the index is in parenthesis it shows where it would appear in the gprof listing if it were to be printed.
Call graph (explanation follows)
granularity: each sample hit covers 4 byte(s) no time propagated
index % time self children called name 0.00 0.00 100/100 tt [3] [1] 0.0 0.00 0.00 100 aa [1] ----------------------------------------------- 0.00 0.00 100/100 tt [3] [2] 0.0 0.00 0.00 100 bb [2] ----------------------------------------------- 0.00 0.00 100/100 main [9] [3] 0.0 0.00 0.00 100 tt [3] 0.00 0.00 100/100 aa [1] 0.00 0.00 100/100 bb [2] -----------------------------------------------
This table describes the call tree of the program, and was sorted by the total amount of time spent in each function and its children.
Each entry in this table consists of several lines. The line with the index number at the left hand margin lists the current function. The lines above it list the functions that called this function, and the lines below it list the functions this one called. This line lists: index A unique number given to each element of the table. Index numbers are sorted numerically. The index number is printed next to every function name so it is easier to look up where the function in the table.
% time This is the percentage of the `total' time that was spent in this function and its children. Note that due to different viewpoints, functions excluded by options, etc, these numbers will NOT add up to 100%.
self This is the total amount of time spent in this function.
children This is the total amount of time propagated into this function by its children.
called This is the number of times the function was called. If the function called itself recursively, the number only includes non-recursive calls, and is followed by a `+' and the number of recursive calls.
name The name of the current function. The index number is printed after it. If the function is a member of a cycle, the cycle number is printed between the function's name and the index number.
For the function's parents, the fields have the following meanings:
self This is the amount of time that was propagated directly from the function into this parent.
children This is the amount of time that was propagated from the function's children into this parent.
called This is the number of times this parent called the function `/' the total number of times the function was called. Recursive calls to the function are not included in the number after the `/'.
name This is the name of the parent. The parent's index number is printed after it. If the parent is a member of a cycle, the cycle number is printed between the name and the index number.
If the parents of the function cannot be determined, the word `<spontaneous>' is printed in the `name' field, and all the other fields are blank.
For the function's children, the fields have the following meanings:
self This is the amount of time that was propagated directly from the child into the function.
children This is the amount of time that was propagated from the child's children to the function.
called This is the number of times the function called this child `/' the total number of times the child was called. Recursive calls by the child are not listed in the number after the `/'.
name This is the name of the child. The child's index number is printed after it. If the child is a member of a cycle, the cycle number is printed between the name and the index number.
If there are any cycles (circles) in the call graph, there is an entry for the cycle-as-a-whole. This entry shows who called the cycle (as parents) and the members of the cycle (as children.) The `+' recursive calls entry shows the number of function calls that were internal to the cycle, and the calls entry for each member shows, for that member, how many times it was called from other members of the cycle.
Index by function name
[1] aa [2] bb [3] tt
#include <stdio.h>
void bb()
{
int a = 0;
for(a = 0 ; a < 10000 ; a++) ;
}
void aa()
{
int a = 0;
for(a = 0 ; a < 10000 ; a++) ;
}
gcov 는 사용하지 않는 함수를 찾는데 유용한 유틸리티 이다.
이녀석을 사용하기 위해서는 컴파일시 -fprofile-arcs -ftest-coverage 두개의 옵션을 줘야 하는데
gcc 문서를 보니 아래와 같이 --coverage 하나만 주어도 무방할 것으로 보인다.(2010.01.24 추가 : --coverage만 해도 된다)
위의 옵션을 주고 컴파일을 하면, [파일명.gcno] 라는 파일이 생성되고,
파일을 실행하면 [파일명.gcda] 라는 파일이 생성된다. gcov [소스파일] 을 입력하면 분석을 한다.
-fprofile-arcs
Add code so that program flow arcs are instrumented. During execution
the program records how many times each branch and call is executed and
how many times it is taken or returns. When the compiled program exits
it saves this data to a file called auxname.gcda for each source file.
The data may be used for profile-directed optimizations
(-fbranch-probabilities), or for test coverage analysis
(-ftest-coverage). Each object file's auxname is generated from the
name of the output file, if explicitly specified and it is not the
final executable, otherwise it is the basename of the source file. In
both cases any suffix is removed (e.g. foo.gcda for input file
dir/foo.c, or dir/foo.gcda for output file specified as -o dir/foo.o).
--coverage
This option is used to compile and link code instrumented for coverage analysis. The option is a synonym for -fprofile-arcs -ftest-coverage (when compiling) and -lgcov (when linking). See the documentation for those options for more details.
-ftest-coverage
Produce a notes file that the gcov code-coverage utility can use to
show program coverage. Each source file's note file is called
auxname.gcno. Refer to the -fprofile-arcs option above for a
description of auxname and instructions on how to generate test
coverage data. Coverage data will match the source files more closely,
if you do not optimize.
프로파일링은 어떤 함수가 몇번이나 불려지고(call), 누가 이 함수를 부르는지(call tree)
그리고 어떤 함수가 실행하는데 오래걸리는지를 분석하는 방법이다.
일반적으로 -pg 옵션을 주고 컴파일 한뒤, 한번 실행하면 프로파일링 파일이 생성된다.(정상종료 되어야 생성됨)
프로그램 실행이후에는 gmon.out 파일이 생성되고, 이 파일을 이용하여 분석한다.
-p
Generate extra code to write profile information suitable for the analysis program prof. You must use this option when compiling the source files you want data about, and you must also use it when linking.
-pg
Generate extra code to write profile information suitable for the analysis program gprof. You must use this option when compiling the source files you want data about, and you must also use it when linking.
문자열은 * 연산을 오버로딩 하고 있고 + 연산으로 여러가지 문장을 합쳐서 출력할수 있다.
(마이너스와 나누기는 안되는듯)
리스트는 배열과 비슷하게 사용이 가능하고
list[0:0] = [value] 이런식으로 리스트의 특정 위치에 값을 추가 할수 있다.
list[0:1] 하면 0번째 에서 1번째 미만의 값을 출력한다.(머 실질적으로 배열에서 0번째 값을 출력하는 셈이다)
리스트를 선언하려면
var = []
라고 선언하고 추후에 추가해주면된다.
사전도 리스트와 비슷하지만
문자열로 값을 찾아야 하고, 값과 숫자를 묶어서 입력해야 한다.
사전을 선언하려면
var = {}
라고 선언하고 추후에 추가해주면된다.
Another useful data type built into Python is the dictionary (see
Mapping Types — dict). Dictionaries are sometimes found in other languages as
“associative memories” or “associative arrays”. Unlike sequences, which are
indexed by a range of numbers, dictionaries are indexed by keys, which can be
any immutable type; strings and numbers can always be keys. Tuples can be used
as keys if they contain only strings, numbers, or tuples; if a tuple contains
any mutable object either directly or indirectly, it cannot be used as a key.
You can’t use lists as keys, since lists can be modified in place using index
assignments, slice assignments, or methods like append() and
extend().
솔찍히 이해를 못한 부분인데, 간단한 함수를 만드는 것 같다.
함수 포인터 같기도 한데 LISP에서 따왔다고도 하는데 먼소리인지 안드로메다로.
Python supports the creation of anonymous functions
(i.e. functions that are not bound to a name) at
runtime, using a construct called "lambda".
>>> def f (x): return x**2 ... >>> print f(8)
64
>>> >>> g = lambda x: x**2 >>> >>> print g(8)
64
As you can see, f() and g() do exactly the same and can be
used in the same ways. Note that the lambda definition
does not include a "return" statement -- it always contains
an expression which is returned. Also note that you can
put a lambda definition anywhere a function is expected,
and you don't have to assign it to a variable at all.
By popular demand, a few features commonly found in functional programming
languages like Lisp have been added to Python. With the lambda
keyword, small anonymous functions can be created. Here’s a function that
returns the sum of its two arguments: lambdaa,b:a+b. Lambda forms can be
used wherever function objects are required. They are syntactically restricted
to a single expression. Semantically, they are just syntactic sugar for a
normal function definition. Like nested function definitions, lambda forms can
reference variables from the containing scope: