Result

Multi-thread

2488647

Round 2 (30) 7 digit

6064838

Single-thread

26097539

Round 2 (30) 8 digit

64478564

Single thread application took 10x

Still 10x because cpu has 6core 12threads

12x will be max but thread context switching will occur. (not exactly x 12)

multi-core application’s can only be 20x as more efficient even if you have infinite number of core count (less efficient when more core is used)

Code

Single-Thread

//Single Thread
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <stdlib.h>

void *heavy_work(void *arg)
{
	(void) arg;
	volatile int num;

	num = 0;
	for (int i = 0; i < 1000000000; i++)
	{
		num += i;
	}
	printf("%d\\n", num);
	return (0);
}

size_t get_time_in_us()
{
	struct timeval	cur;
	gettimeofday(&cur, NULL);
	return (cur.tv_sec * 1000000 + cur.tv_usec);
}

int main()
{
	pthread_t thread[30];
	size_t start = get_time_in_us();
	for (int i = 0; i < 30; i++)
		heavy_work(0);
	start = get_time_in_us() - start;
	printf("time_taken: [%zu]\\n", start);
}

Multi-Thread

#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <stdlib.h>

void *heavy_work(void *arg)
{
	(void) arg;
	volatile int num;

	num = 0;
	for (int i = 0; i < 1000000000; i++)
	{
		num += i;
	}
	printf("%d\\n", num);
	return (0);
}

size_t get_time_in_us()
{
	struct timeval	cur;
	gettimeofday(&cur, NULL);
	return (cur.tv_sec * 1000000 + cur.tv_usec);
}

int main()
{
	pthread_t thread[30];
	size_t start = get_time_in_us();
	for (int i = 0; i < 30; i++)
	{
		pthread_create(&thread[i], NULL, heavy_work, NULL);
	}
	for (int i = 0; i < 30; i++)
	{
		pthread_join(thread[i], NULL);
	}
	start = get_time_in_us() - start;
	printf("time_taken: [%zu]\\n", start);
}

Fault in testing