K2 Language

Execution in Nanoseconds

A minimalist programming language designed for ultra-fast execution with operations completing in 70 nanoseconds to 9 milliseconds.

example.k2
# K2 Example
x = 10
y = 20
sum = x + y
print sum

# Execution time: 70 nanoseconds

Features

Ultra-Fast

Execute operations in 70-9000 nanoseconds, making K2 one of the fastest interpreted languages.

Lightweight

Minimal footprint with the entire interpreter under 30KB, perfect for resource-constrained environments.

Simple Syntax

Clean, intuitive syntax that's easy to learn and use, focusing on readability and performance.

Built-in Timing

Integrated execution time measurement for performance monitoring and optimization.

Command Line

Run programs from files or execute single expressions directly from the command line.

No Dependencies

Zero external dependencies, making installation and deployment simple across platforms.

Performance

K2 is designed for speed. Here's how it compares to other languages:

70 ns

Fastest operation

~500 ns

Average operation

9 ms

Complex operations

Benchmark Results

Operation K2 Python JavaScript Ruby
Variable assignment 100-400 ns ~10,000 ns ~5,000 ns ~15,000 ns
Integer addition 70-200 ns ~8,000 ns ~3,000 ns ~12,000 ns
Print operation 1,000-2,000 ns ~50,000 ns ~30,000 ns ~60,000 ns
Complex expression 300-5,000 ns ~100,000 ns ~80,000 ns ~150,000 ns

Nahir UI Framework

Experience the next generation of K2 programming with our new Nahir UI framework.

Accelerated Rendering

Nahir UI components render in microseconds, providing instant visual feedback and smooth animations.

Component System

Build complex interfaces with reusable, composable components that maintain K2's performance benefits.

Ramdisk Caching

Utilizes the new K2 ramdisk cache system for ultra-fast component state management and rendering.

Responsive Design

Automatically adapts to any screen size with zero performance penalty, perfect for mobile applications.

nahir-example.k2
# Nahir UI Component Example
component Button {
  props = {
    text: "Click Me",
    color: "blue",
    onClick: null
  }
  
  render {
    element = "button"
    style = {
      background: props.color,
      padding: "10px 20px",
      borderRadius: "4px"
    }
    content = props.text
    events = {
      click: props.onClick
    }
  }
}

# Create and render a button
myButton = Button.new({
  text: "Submit",
  color: "green",
  onClick: function() {
    print "Button clicked!"
  }
})

UI.render(myButton)

Live Preview

Console
Button clicked!
Execution time: 85 nanoseconds

Interactive preview of the Nahir UI component system

Explore Nahir UI View Examples

K2 GTK GUI Library

Build powerful native desktop applications with our high-performance GTK4-based GUI library.

Native Performance

Create applications that look and feel native on Linux, Windows, and macOS with the performance of compiled code.

Simple API

Designed specifically for K2, the API is intuitive and easy to learn, making GUI development accessible to everyone.

Flexible Layouts

Create complex user interfaces with flexible box and grid layouts that adapt to different screen sizes.

Event Handling

Respond to user interactions with a simple callback system that makes event handling straightforward.

gtk-example.k2
# K2 GTK GUI - Hello World Example
import GtkGui

# Create a new application
app = GtkGui.Application.new("org.k2lang.gtkgui.hello", "Hello World")

# Create a window
window = GtkGui.Window.new(app, "Hello World", 400, 300)

# Create a vertical layout container
layout = GtkGui.Box.new()
layout.setOrientation(GtkGui.ORIENTATION_VERTICAL)
layout.setSpacing(20)
layout.setMargin(20)

# Create a heading label
heading = GtkGui.Label.new()
heading.setText("Hello, World!")
heading.setProperty("markup", "Hello, World!")

# Create a button
button = GtkGui.Button.new()
button.setLabel("Click Me")

# Add event listener to the button
button.setCallback("clicked", function(widget) {
  GtkGui.Dialog.showMessage(window, "Hello", "You clicked the button!")
})

# Add components to the layout
layout.add(heading)
layout.add(button)

# Set the window content
window.setContent(layout)

# Show the window
window.show()

# Start the application
app.run()

Native GTK Application

Hello World
Hello, World!

Preview of a native desktop application built with K2 GTK GUI

Explore K2 GTK GUI View Demo

Examples

basic.k2
# Basic K2 program
x = 42
print x

Output:

42
Execution time: 1230 nanoseconds

This simple example shows variable assignment and printing. K2 automatically displays execution time.

arithmetic.k2
# Arithmetic operations
a = 10
b = 5

sum = a + b
diff = a - b
product = a * b
quotient = a / b

print sum
print diff
print product
print quotient

Output:

15
Execution time: 1150 nanoseconds
5
Execution time: 1120 nanoseconds
50
Execution time: 1130 nanoseconds
2
Execution time: 1140 nanoseconds

K2 supports all basic arithmetic operations with extremely fast execution times.

fibonacci.k2
# Fibonacci sequence
a = 0
b = 1

print a
print b

c = a + b
print c
a = b
b = c

c = a + b
print c
a = b
b = c

c = a + b
print c

Output:

0
1
1
2
3

This example calculates the first few numbers in the Fibonacci sequence, demonstrating variable reassignment.

timing.k2
# Execution time control
x = 100
y = 200

# Show execution time
print x + y

# Disable execution time display
show_exec_time off
print x * y

# Enable execution time display
show_exec_time on
print x - y

Output:

300
Execution time: 1250 nanoseconds
20000
-100
Execution time: 1180 nanoseconds

K2 allows you to toggle execution time display, which is useful for benchmarking and performance analysis.

Online K2 Runner

Try K2 directly in your browser without installing anything. Write your code and see the results instantly.

K2 Code Editor
Output
// Output will appear here when you run your code
Execution time: 0 ns
Memory usage: 0 KB

Example Snippets

Native x86_64 Execution

This online runner executes your K2 code using the same precompiled x86_64 binary that powers the desktop version, providing authentic performance metrics.

Ultra-Fast Performance

Experience K2's nanosecond-level execution speed directly in your browser. The code is executed on our servers using optimized native binaries.

Secure Sandbox Environment

All code is executed in an isolated environment with strict resource limits and security controls to ensure safety.

Documentation

Installation

Installing K2 is simple and straightforward:

# Download the package
git clone https://github.com/JJPEOPLES/k2lang.git
cd k2lang

# Compile
g++ -std=c++17 -O3 k2.cpp -o k2

# Install globally (optional)
sudo cp k2 /usr/local/bin/

For Debian/Ubuntu/Kali Linux users, you can use the provided Debian package:

sudo dpkg -i k2-language_1.0.0-1_amd64.deb

Usage

Running K2 programs is easy:

# Run a K2 program file
k2 program.k2

# Execute a single expression
k2 -e "print 100 + 200"

Syntax

K2 has a clean, minimal syntax:

  • Comments start with #
  • Statements are written one per line
  • Variables are assigned with =
  • Output is done with print

Variables

Variables in K2 store integer values:

# Variable assignment
x = 10
y = 20

# Using variables
z = x + y

Operations

K2 supports these arithmetic operations:

  • Addition: a + b
  • Subtraction: a - b
  • Multiplication: a * b
  • Division: a / b

Commands

Special commands in K2:

  • print: Outputs a value
  • show_exec_time: Controls execution time display (on/off)

Download K2

Source Code

Get the latest source code from GitHub

Download Source

Linux Package

Debian/Ubuntu/Kali Linux package

Download .deb

Binary Release

Pre-compiled binary for Linux x86_64

Download Binary

Documentation

Complete K2 language documentation

View Documentation

Example Programs

Collection of K2 example programs

Download Examples

Installation Script

Automated installation script for Linux

Download Script

Try K3 - The Next Generation

Experience the next evolution of K2 with improved performance, memory safety, and the Stellar UI framework

Explore K3

About Prime

Prime (also known as Tera) is the creator of the K2 and K3 programming languages. A passionate developer and systems thinker, Prime focuses on building ultra-fast, minimal, and expressive tools for the next generation of programmers. From compiler design to full-stack deployment, Prime engineers every piece with care, precision, and blazing performance in mind.