Reputation: 1319
I need some help with external program call from c++ code.
I have to call javap.exe
(from JDK package) from my program many times (probably more than 100), but call system("javap.exe some_parameters")
is extremely slow. It's work so good for one set of parameters but repeated calls of system()
not acceptable. I think it is only because of costs to access the hard disk and application run (but I'm not sure).
What can I do for better performance? Can I "save javap.exe
in RAM" and call it "directly".
Or may be somebody knows how can I get java-class description and methods signature without javap.exe
?
Upvotes: 3
Views: 321
Reputation: 39807
Calling system()
is easy, but very inefficient, primarily because you are not just launching whatever program you specify. Rather, you are launching one process (a shell), and that shell will examine your parameter and launch a second process.
If you're on a system that supports fork()
and exec*()
, you're going to improve performance by using them instead. As a pseudo-code example, consider:
void replace_system(const char *command)
{
pid_t child = fork();
if (child < 0) {
perror("fork:");
return;
}
if (child) {
/* this is the parent, wait for the child to finish */
while (waitpid(child, &status, options) <= 0);
return;
}
/* this is the new process */
exec*(...);
perror("failed to start the child");
exit(-1);
}
Choose one of the exec* functions based on how you want to arrange the parameters. You'll need to break your string of arguments into components, and possibly provide an environment of your liking. Once you call the exec* function, that function will never return (unless there is an error starting the command you've defined for it).
Beyond performance considerations, another reason to use this is, if desired, it allows you to modify the child's standard paths. For example, you might be interested in the output of a child; if you modify its stdout to be a pipe available to you, you can simply read what it prints. Research source code for the standard popen()
call to find an example of this.
Upvotes: -1
Reputation: 41474
The Java VM is not cheap to start running, and it's likely that its initialization is eating up the lion's share of your time. Luckily, the functionality of javap
is available directly through Java code. I suggest that you write a small Java application which, while similar to javap
, does with one invocation what you would otherwise need thousands for. (Though... maybe you could already use just one? javap will take multiple class files, after all...)
Upvotes: 6