0% found this document useful (0 votes)
4 views16 pages

Material and methods

Uploaded by

personalnon85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Material and methods

Uploaded by

personalnon85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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

Simple queue using C


C language considered as from the fastest programming languages in the world.
As shown in fig 1 we had to define an array called “queue” and her size to add elements at
here.
And had to define to variable “front & rear”:
1. front: to pointer on the first element in the array.
1
2. rear: to pointer on the last element in the array.

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”.

Result: As shown in fig[7] the operation takes 0.045 milliseconds to finish.

Simple queue using Java


Java considered from the modern programming languages.

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

Show fig [9].

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:

 Initially, it checks whether the queue is full.

o If the queue is not full:

 If front is undefined (i.e., no elements are currently in the queue),


both front and rear are initialized to the first index.

 Otherwise, rear is incremented to the next index, and the new


element is inserted at that position.

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 elements are present:

 front is advanced to the next index.

 A subsequent check is performed to determine if front has moved


beyond rear.

 If so, both front and rear are reset to an undefined state,


indicating that the queue is now empty.

o If the queue is already empty, the function outputs the message: "Queue is
empty".

3. Peek Operation

Show fig [10].

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.

o If the condition holds, the value at the front index is returned.

o If the queue is empty, an appropriate message is displayed: "Queue is


empty".

4. Display Operation

Show fig [11].

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:

 A loop is executed starting from the front index up to rear, printing


each value.

o If no elements exist, the message "Queue is empty" is printed.

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:

 front: A pointer to the first element in the queue.

 rear: A pointer to the last element in the queue.

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

As illustrated in Figure [15], we declared a function called "enqueue" with a parameter


"value".
The function follows these steps:

 It checks if the queue is full:

o If true:

 Display the message: "Queue is full".

o If false:

 If the queue is initially empty (front == -1 and rear == -1):

 Set both front and rear to 0.

 Insert the value at this position.

 Otherwise:

5
 Update rear as (rear + 1) % size.

 Insert the value at the updated rear position.

2. Dequeue Function

Show fig [16].

The "dequeue" function is responsible for removing the first element from the queue.
Its operation is as follows:

 It checks if the queue is empty (front == -1):

o If true:

 Display the message: "Queue is empty".

 Otherwise:

o If there is only one element (front == rear):

 Reset both front and rear to -1.

o Else:

 Update front as (front + 1) % size.

3. Peek Function

Show fig [17].

The "peek" function returns the value of the front element without removing it.
The procedure is:

 It checks if the queue is not empty:

o If true:

 Return the value at the front index.

o If false:

 Display the message: "Queue is empty".

4. Display Function

6
Show fig [18].

The "display" function prints all current elements in the queue.


The process is:

 If the queue is not empty:

o If front <= rear:

 Use a loop to print elements from front to rear.

o If front > rear:

 Print 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".

Result

As shown in Figure [20], the circular queue operations in C were executed successfully,
achieving a runtime of approximately 0.068 milliseconds.

Circular Queue Using Java

Java is considered one of the modern and versatile programming languages.


As demonstrated in Figure [21], a circular queue was implemented using an array called
"queue". Two auxiliary variables were defined:

 front: A pointer indicating the position of the first element.

 rear: A pointer indicating the position of the last element.

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:

 Display the message: "Queue is full".


o If false:

 If the queue is empty (front == -1):


 Set both front and rear to 0.
 Otherwise:
 Update rear as (rear + 1) % size.
 Insert the value at the updated rear position.

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:

 Display the message: "Queue is empty".


 Otherwise:
o If only one element exists (front == rear):

 Reset front and rear to -1.


o Else:

 Update front as (front + 1) % size.

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:

 Display the message: "Queue is empty".

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:

 Loop from front to rear and print each element.


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.

3.Result & Descusion


3.1 Introduction

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

Memory Usage (in kilobytes)

Input Size C (Array Queue) Java (Array Queue)

1,000 40 60

5,000 200 270

10,000 400 520

50,000 2000 2600

Explanation of the implementation time table :


C outperforms:

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.

Explanation of the memory usage table :


Memory efficiency in C:

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.

Throughput (Operations per Second)

Input Size C (ops/sec) Java (ops/sec)

1,000 200,000 83,333

5,000 277,777 106,382

10,000 285,714 111,111

50,000 294,117 111,111

Throughput Comparison (Table 3.3)


1. Definition of Throughput:

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.

Difference: C is 2.4x faster on average.

2.Data size effect:

As the input size increases, Throughput improves in both languages because:

Reduce the overhead of static operations (such as function calls).

Improved cache usage when working with large data.

3.Maximum productivity:

We note that Throughput stabilizes at:

111,111 operations/sec for Java.

290,000 operations/s for C.

Cause: The system has reached maximum CPU/memory processing power.

Efficiency Ratio (C Speed / Java Speed)

12
Input Size Efficiency Ratio

1,000 2.4x

5,000 2.6x

10,000 2.57x

50,000 2.65x

Relative efficiency between C and Java.

Efficiency Ratio (Table 3.4)


1. Definition:
o Measures how many times C is faster than Java for the same task.

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.

o Lower memory overhead and direct memory management.

o Suitable for systems requiring high performance and low-level hardware


interaction.
 Cons:
o Requires manual memory management, increasing the risk of memory
leaks.
o More prone to errors such as pointer mistakes.

Java Language Implementation:


 Pros:
o Easier and safer memory management due to automatic garbage
collection.
o Rich standard library and built-in support for dynamic data structures.

 Cons:
o Higher execution time due to the Java Virtual Machine (JVM) layer.

o Larger memory footprint compared to C.

Discussion :

From the results:


 C consistently outperforms Java in terms of execution speed.
 As the input size increases, the performance gap between C and Java widens
slightly.
 Java, while slower, offers advantages in terms of programming safety, ease of
debugging, and portability, making it preferable for large, maintainable projects.
The comparative study between the array-based queue implementations in C
and Java concludes that:
 C provides superior performance and memory efficiency, ideal for performance-
critical applications.
 Java provides better developer productivity and safety, at the cost of slightly
higher execution time.

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).

o Java: ArrayDeque is not thread-safe; ConcurrentLinkedQueue is


preferred for concurrency.
3.3 Practical Recommendations
1. For High Performance: Use C in embedded systems or real-time applications.
2. For Flexibility: Prefer Java with ArrayList-backed queues or libraries
like ArrayDeque.
3. For Large Data: Implement circular queues to optimize space reuse in both
languages.

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

You might also like