Material and methods
Material and methods
Materials:
Experiment Overview (Queue Implementation):
This experiment was conducted to evaluate and compare the performance of queue operations
(enqueue, dequeue, peek, and display) using two different programming languages: C and
Java. The goal was to determine which language provides faster and more efficient execution
for basic queue operations.
The tools and environment used for the experiment included:
A laptop equipped with an Intel Core i7-4702MQ processor and 8 GB RAM.
Programiz online compiler for both C and Java implementations.
Windows 10 operating system.
In this setup, identical queue operations were implemented separately in C and Java. The
performance of both versions was measured and compared to analyze differences in
execution speed and overall behavior.
Methods:
To do the experiment we had to use two variables called “front & rear” and use some
functions:
1. Clock
2. Enqueue
3. Dequeue
4. Peek
5. Display
Simple queue
Simple queue approved you to add and remove elements but if you remove element, you
can’t add another one again.
5 10 15 48 25
1 – Enqueue function:
As shown in fig [2] we have declared function called “enqueue” with parameter called
“Value”.
First, checked if the queue was full:
if true: checked if front doesn’t pointer on something in the array:
o if true: made front and rear pointer on the first index in queue.
o If false: made rear pointer on the next index and add element on him.
If false: print to user “Queue is full”.
2 – Dequeue function:
Show fig [3].
declared function called “dequeue”; This function used to remove first element in the
array.
First, checked if there are elements in the array:
If true:
o made front pointer on the next index
o checked if front pointer on the index after that rear pointer on him:
if true: made rear and front pointer out the queue.
If false:
o Print to user “Queue is empty”
3 – Peek function:
Show fig [4].
Declared return function called “peek” to return first element in the queue.
First, checked if there are element in queue and the index of front <= index of rear:
If true:
o Return the value of index that front pointer on him.
If false:
o Print to user: “Queue is empty”.
4 – Display function:
2
Show fig [5].
Declared function called “Display function” to print the element in the queue.
First, checked if there are element in queue and the index of front <= index of rear:
If true:
o Made for loop started from the index that front pointer on him until
reached to rear
o Print every value we pass on here.
If false:
o Print to user “Queue is false”.
In this project, as illustrated in Figure [8], we implemented a basic queue data structure
using an array named queue. Two auxiliary variables were defined to manage the queue
operations:
front: a pointer indicating the position of the first element in the queue.
rear: a pointer indicating the position of the last element in the queue.
1. Enqueue Operation
The enqueue function was designed to add new elements to the queue. It accepts a
parameter Value, which represents the element to be inserted. The function follows these
steps:
o If the queue is full, a message is displayed informing the user: "Queue is full".
3
2. Dequeue Operation
The dequeue function handles the removal of elements from the front of the queue. The
procedure is as follows:
The function first checks whether any elements exist in the queue.
o If the queue is already empty, the function outputs the message: "Queue is
empty".
3. Peek Operation
The peek function provides a way to view the element at the front of the queue without
removing it. Its operation is as follows:
It verifies whether the queue is not empty and that front is less than or equal to rear.
4. Display Operation
The display function is responsible for printing all the current elements in the queue. The
function operates as follows:
It checks if the queue contains any elements and ensures front is less than or equal
to rear.
4
o If elements are found:
Execution Results
As demonstrated in Figure [13], the queue operations were executed efficiently, achieving a
total runtime of approximately 1.215 milliseconds.
Circular queue
Circular Queue Using C
The C language is considered one of the fastest programming languages in the world.
As shown in Figure [14], we defined an array called "queue" with a specified size to store
elements. Additionally, two variables were defined:
In a circular queue, when rear reaches the end of the array and space is available at the
beginning (due to previous dequeues), rear wraps around to the start of the array, providing
better memory utilization.
1. Enqueue Function
o If true:
o If false:
Otherwise:
5
Update rear as (rear + 1) % size.
2. Dequeue Function
The "dequeue" function is responsible for removing the first element from the queue.
Its operation is as follows:
o If true:
Otherwise:
o Else:
3. Peek Function
The "peek" function returns the value of the front element without removing it.
The procedure is:
o If true:
o If false:
4. Display Function
6
Show fig [18].
Print from front to the end of the array, then from the beginning to
rear.
Result
As shown in Figure [20], the circular queue operations in C were executed successfully,
achieving a runtime of approximately 0.068 milliseconds.
The circular nature of the queue ensures that when rear reaches the end of the array, it
loops back to the start if space is available, thus optimizing memory usage.
1. Enqueue Operation
Show fig [22].
7
The "enqueue" function is designed to insert a new element into the queue.
The operation follows these steps:
Check if the queue is full:
o If true:
2. Dequeue Operation
Show fig [23].
The "dequeue" function handles the removal of elements from the queue:
Check if the queue is empty:
o If true:
3. Peek Operation
Show fig [24].
The "peek" function allows viewing the first element without removing it.
The function works as follows:
Verify if the queue is not empty:
o If true:
8
Return the value at the front index.
o If false:
4. Display Operation
Show fig [25].
The "display" function prints all elements currently in the queue:
If the queue is not empty:
o If front <= rear:
Print elements from front to the end of the array, then from the
beginning to rear.
If the queue is empty:
o Display the message: "Queue is empty".
Execution Results
As demonstrated in Figure [27], the circular queue operations in Java were completed
successfully, achieving a runtime of approximately 0.983 milliseconds.
9
This extended analysis provides deeper insights into queue performance using arrays in C and
Java,The aim is to evaluate their efficiency by measuring the execution time and memory
consumption under different input sizes, and to interpret how language-specific features
affect queue performance in practical scenarios, featuring additional tables and visualizations
to better illustrate the differences.
3.2 Results
The array-based queues were tested by performing a series of enqueue and dequeue
operations on datasets of varying sizes. The execution time (in milliseconds) and memory
usage (in kilobytes) were recorded for both C and Java implementations.
Execution Time (in milliseconds)
Java(array queue) C(array queue) Input size
12 5 1000
47 18 5000
90 35 10000
450 170 50000
Table (3.1) Execution time for enqueue + dequeue operations in C and Java
1,000 40 60
Enqueue and dequeue operations are performed 60% faster compared to Java.
Example: With 10,000 elements, C needs 35ms while Java needs 90ms.
Linear Scaling
Both languages show a linear increase in execution time with increasing data size. Every
5x increase in input size results in a corresponding increase in time (5x).
Reasons for the difference:
10
C: Works directly with memory (no intermediate layer like JVM).
Java: overhead exists because:
Automatic memory management (Garbage Collection).
Bounds Checking in arrays.
C consumes 30% less memory than Java for the same size of data.
Example:
With 50,000 elements, C uses 2000KB while Java uses 2600KB.
Reasons for the difference:
Java: Needs extra memory because, Object Headers for each element.
The ArrayQueue data structure contains additional fields (such as size, modCount).
C: Arrays are used directly without padding or metadata.
Fixed style:
The 30% is roughly constant regardless of the size of the input.
11
o Measures how many queue operations (enqueue/dequeue) can be
performed per second.
o Formula:
Input SizeExecution
Throughput =
Time(seconds)
1.Superior C performance:
C reaches 200,000 operations/sec for 1,000 objects, while Java reaches only 83,333.
3.Maximum productivity:
12
Input Size Efficiency Ratio
1,000 2.4x
5,000 2.6x
10,000 2.57x
50,000 2.65x
o Formula:
Java Execution Time
Efficiency Ratio =
C Execution Time
2. Trends:
o C consistently outperforms Java by 2.4–2.65×, regardless of input size.
o The ratio slightly increases with larger datasets (e.g., 2.65× at 50k
elements) due to:
C’s predictable memory allocation.
Java’s GC pauses becoming more frequent.
3. Implications:
o For large-scale systems, C’s efficiency translates to lower hardware
costs and better scalability.
o Java’s minor performance trade-off may be acceptable for developer
productivity.
C Language Implementation:
Pros:
13
o Faster execution due to being compiled directly to machine code.
Cons:
o Higher execution time due to the Java Virtual Machine (JVM) layer.
Discussion :
14
The choice between the two languages depends on project requirements:
If speed and efficiency are critical → C is preferred.
If portability, maintainability, and development speed are priorities → Java is a
better fit.
Limitations
Fixed Capacity: Both implementations suffer from fixed-size arrays, leading to:
o C: Manual resizing requires reallocation (realloc()), risking memory
fragmentation.
o Java: ArrayQueue needs dynamic resizing (e.g., Arrays.copyOf), which is
costly.
Thread Safety:
o C: Not thread-safe by default (requires mutex locks).
It is possible that there may be conflicts or differences between the submitted report and
some other reports or other research, and one of the reasons for this difference is the
difference in time between research or when using different materials. Therefore, we
must clarify that this report may require additional studies or improvement of the work
method.
15
16