I have following program.
// gcc -o slowdown slowdown.c -lngspice -lpthread -Wall -ggdb
#include <stdio.h>
#include <stdbool.h>
#include <ngspice/sharedspice.h>
#include <sys/time.h>
#include <unistd.h>
int myControlledExit(int exitstatus, bool immediate, bool quitexit, int ident, void *userdata) {
printf("exit: es=%d im=%d qe=%d\n", exitstatus, immediate, quitexit);
return exitstatus;
}
long long millis() {
struct timeval te;
gettimeofday(&te, NULL);
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000;
return milliseconds;
}
int main() {
ngSpice_Init(NULL, NULL, myControlledExit, NULL, NULL, NULL, NULL);
long long t1 = millis(), t2;
char *a[] = {
(char*)"* first line is ignored",
(char*)"V1 1 0 9",
(char*)"RR1 1 3 100",
(char*)"RR2 3 0 100",
(char*)".end",
NULL
};
for (int i = 0; i < 100000; i++) {
ngSpice_Circ(a);
ngSpice_Command((char*)"tran 100u 20m 0");
ngSpice_Command((char*)"destroy all");
if (i % 1000 == 999) {
t2 = millis();
printf("iter %d, duration %lld\n", i, t2 - t1);
t1 = t2;
}
}
return 0;
}
When I ran it it gradually slows down:
iter 999, duration 2262
iter 1999, duration 2423
iter 2999, duration 2715
iter 3999, duration 3096
How can I make it not slow down?
I don't think that this is a bug.
When you run
ngSpice_Circ(a);
, you are creating a new circuit. When you runtran 100u 20m 0
, you are creating a new plot storing the simlation results. When you rundestroy all
, you are removing the simulation results.The circuits stay stored in ngspice.
So after the loop has finished, you have 100000 circuits loaded. If you need all of them, you will have to live with the slowdown. If not, make use of the command
remcirc
.Only when this does not help, we should profile ngspice to find the source of the then still occuring slowdown.
remcirc solved the issue, thanks.