Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intergrating C into Perl

Status
Not open for further replies.

programingnoob

Technical User
Dec 31, 2002
71
CA
Hello,

What is the fastest way to make a Perl script use a C program? And how do I do it?

Thanks
 
Code:
$results=`path/to/cprog -rpmfad`;
using a lot of switches, basically the stuff between the backticks is passed to the shell, and the results returned

other options include system(), and exec(), but I think one of these forks, and you can't get the output

Look em all up, theyre grrr8

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Yes I know those are the options, however, I will be calling the C programing a few thousand times. I am trying to find a way that will cost as little time as possible.
 
What does the C ?executable? do?

How long is it taking at present?

Do you have the C source? Did you write it yourself? Could it be implemented in Perl? A few thousand times, in what context?

Just a few of the questions I'd be asking

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
1) the c exe does a very complex but fast mathematical calculation.
2) It takes about 10ms to run.
3) i will not be porting it to perl.
4) no i didnt write it myself
5) i was asking you that :)
6) It will be running a few thousand calculations.

I just want to know which way of calling the program would use the least time. I can edit the C program accordingly if its needed to speed things up.
 
I'd look into threading, though multiple complex calcs could bottleneck the processor, and there may be no advantage over pipelining.

10ms isn't a long time - unless it's a very powerful processor, but when using a multiplier of 1000's, then it's going to start to lag, depending on what else the processor has in it's schedule

5) Are you going to be running this all day every day, or after the results of a test, or a stock quote change, or in a batch ... this is what I meant by context, also if you're on a shared webhost, your cpu count could call in the hosting company to ask what you're doing

Just trying to help
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
This will be used only in an interval of 1 minute or so. Well, I am actually pulling out a huge table from Mysql. Afterwards, I will be updating another table according the calculations from the previous table. Well, 10ms is the worse possible case. And 1000s is also too. :)
 
No worries so, as long as you're not drawing heat

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Oopz sorry :p
I meant back to the question & <-- how do I thread.
sorry i forgot about it.

Well, wouldn't I waste time activationg the exe?
 
My original plan was to do something like a system call

program [switches] [arguemnts]
something liek that.
 
If a job needs to be done, let it off, if it causes a problem, then worry, unless you're already anticipating a lot of backlash from co-workers.

You could move to asp.NET, and recode everything in C-Sharp, and COBOL and Perl and Basic, and QuickBasic <-- that would be wasting time.

your computer (server or your local desktop?) has a job to do, let it run and see how it behaves.

Given the answers you've supplied, I'm thinking that you're running scenarios on pricing structures to get yield curves.

If this is the case, and it's just for you and no-one else, you shouldn't have a problem, get a budget for a new PC, or just go have coffee and a cigarette when you're running this.

On a production server, you'd be taking a brave bit of time on one user kicking this off, but for a number of users, you'd be doing the machine a kindness by pulling the plug. This would be a DOS dream.

Is there any possibility of setting up a matrix of values, (it could be calculated on a different machine), and switch in the new values when the population is completed, and leave the webserver (if it is one) to handle serving data and pages rather than data, pages and calcs

Just my €0.02
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Lol this is a very long answer :p no this has nothing to do with stocks hahahaha. :p so whats the best way for the c exe to work and the perl to call the c exe (in terms of time).

i thought that perhaps i get the C program to accept commands from STDIN so i would cut the overhead of the starting the program. but any other ideas?
 
exec forks - fire'n'forget - great if you don't care about any errors
system waits for the process to complete
`` wait for the process to complete, BUT gives you output that you can parse as opposed to trying to decipher the return code for system


You've got to get input from somewhere, I dont think where you get the input from will have any realisable overhead in comparison to the CPU count if C gets it, or Perl gets it. The only benefit of C getting fed the commandline options, and you not having control over that source, you can log all your operations and results - that's if you wnated to

--Paul




It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Okay. So you mean in perl...........
doing this 1000 times
program [switches] [arguemtns]

and doing this once
program [switches]

and this 1000 times
-args (stdin)

is the same in terms of time?
 
I think that without having all the information it's impossible to say what your best line of attack should be.

--Paul

PS have a look at threading and let me get some sleep ;-)


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Okay i was looking in threading. Its pretty good.Especially when the script is ran on a Duel Xeon 2.8
 
With a dual processor machine (do those xeons have HT?) you definitely want more than one thread/process running at once.

My first thought when you mentioned it was a C program was to actually embed it in Perl. Perl was written in C and can be extended with C. You didn't write the C, but do you have access to the source?

Perl man pages on embedding C in Perl swiped from perlembed: perlxstut perlxs h2xs perlguts perlapi

And some forking/threading examples from DS. Just change &spawn_kid to make a system() call and the constant at the top (drop most of the prints elsewhere, too) and you should be in business.

________________________________________
Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top