简体   繁体   中英

Plotting time against size of input for Longest Common Subsequence Problem

I wish to plot the time against size of input, for Longest common subsequence problem in recursive as well as dynamic programming approaches. Until now I've developed programs for evaluating lcs functions in both ways, a simple random string generator(with help from here ) and a program to plot the graph. Now I need to connect all these in the following way.

Now I have to connect all these. That is, the two programs for calculating lcs should run about 10 times with output from simple random string generator given as command line arguments to these programs.

The time taken for execution of these programs are calculated and this along with the length of strings used is stored in a file like

l=15, r=0.003, c=0.001 

This is parsed by the python program to populate the following lists

sequence_lengths = [] 
recursive_times  = []
dynamic_times    = []

and then the graph is plotted. I've the following questions regarding above.

1) How do I pass the output of one C program to another C program as command line arguments?

2) Is there any function to evaluate the time taken to execute the function in microseconds? Presently the only option I have is time function in unix. Being a command-line utility makes it tougher to handle.

Any help would be much appreciated.

If the data being passed from program to program is small and can be converted to character format, you can pass it as one or more command-line arguments. If not you can write it to a file and pass its name as a argument.

For Python programs many people use the timeit module's Timer class to measure code execution speed. You can also roll-you-own using the clock() or time() functions in time module. The resolution depends on what platform you're running on.

1) There are many ways, the simplest is to use system with a string constructed from the output (or popen to open it as a pipe if you need to read back its output), or if you wish to leave the current program then you can use the various exec (placing the output in the arguments).

In an sh shell you can also do this with command2 $(command1 args_to_command_1)

2) For timing in C, see clock and getrusage .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM